spring boot for swagger 如何根据请求类型为请求模型动态定义参数列表
How to define parameter list dynamically based on request type for request model in spring boot for swagger
我正在使用 spring boot 的 Rest Controller 来创建 rest 端点。与 api 文档的 swagger 2 一起。
@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, System.currentTimeMillis(), HttpStatus.BAD_REQUEST);
}
}
@PutMapping("/batch")
public GeneralResponse<Boolean> updateBatch(@RequestBody Batch batch) {
try {
batchService.updateBatch(batch);
return new GeneralResponse<>(true, "batch updated successfully", true, System.currentTimeMillis(), HttpStatus.OK);
} catch (Exception e) {
return new GeneralResponse<>(false, e.getMessage(), false, System.currentTimeMillis(), 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;
}
我正在使用 spring boot 的 Rest Controller 来创建 rest 端点。与 api 文档的 swagger 2 一起。
@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, System.currentTimeMillis(), HttpStatus.BAD_REQUEST);
}
}
@PutMapping("/batch")
public GeneralResponse<Boolean> updateBatch(@RequestBody Batch batch) {
try {
batchService.updateBatch(batch);
return new GeneralResponse<>(true, "batch updated successfully", true, System.currentTimeMillis(), HttpStatus.OK);
} catch (Exception e) {
return new GeneralResponse<>(false, e.getMessage(), false, System.currentTimeMillis(), 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;
}