如何解决如何在Swift Boot的Spring Boot中基于请求模型的请求类型动态定义参数列表
我正在使用Spring Boot的Rest Controller创建休息终点。带有swagger 2的api文档。
@RestController
@RequestMapping("/api")
public class BatchController extends ControllerConfig {
@PostMapping("/batch")
public GeneralResponse<Boolean> createBatch(@RequestBody Batch batch) throws Exception{
try{
batchService.createBatch(batch);
return new GeneralResponse<>(true,"batch created successfully",true,System.currentTimeMillis(),HttpStatus.OK);
} catch (Exception e){
return new GeneralResponse<>(false,e.getMessage(),false,HttpStatus.BAD_REQUEST);
}
}
@PutMapping("/batch")
public GeneralResponse<Boolean> updateBatch(@RequestBody Batch batch) {
try {
batchService.updateBatch(batch);
return new GeneralResponse<>(true,"batch updated successfully",HttpStatus.OK);
} catch (Exception e) {
return new GeneralResponse<>(false,HttpStatus.BAD_REQUEST);
}
}
}
和批处理模型:
@Entity
@Table
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Batch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long qualityId;
private Date date;
private String remark;
}
我正在使用JPA存储库。
现在,对于其余两个端点,Swagger会将请求模型显示为:
{
id: 0,qualityId: 0,date: "2020-10-04T21:18:00.656Z",remark: "string"
}
但是我想隐藏用于自动生成批处理请求的“ id”字段,但由于它是基于id的,因此需要更新。
那怎么办?
解决方法
不应在API层中公开实体, 您应该改为创建专用的DTO类。
例如-
@Data
public class PutBatchDTO {
private Long id;
private Long qualityId;
private Date date;
private String remark;
}
@Data
public class PostBatchDTO {
private Long qualityId;
private Date date;
private String remark;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。