英文:
How to define parameter list dynamically based on request type for request model in spring boot for swagger
问题
@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);
}
}
}
Batch Model:
@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;
}
For both the rest endpoints, Swagger will show the request model as:
{
qualityId: 0,
date: "2020-10-04T21:18:00.656Z",
remark: "string"
}
However, the "id" field should be hidden for the create batch request since it's autogenerated, but it's required for the update request as it's based on the id. To achieve this, you may need to customize the Swagger documentation.
英文:
I am using spring boot's Rest Controller for creating rest end points. Along with swagger 2 for api documentation.
@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);
}
}
}
And Batch Model :
@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;
}
I am using JPA repository.
Now, For both the rest end points Swagger will show the request model as :
{
id: 0,
qualityId: 0,
date: "2020-10-04T21:18:00.656Z",
remark: "string"
}
but I want to hide "id" field for create batch request as that is autogenerated, but its required for update as that is based on id.
how can that be done?
答案1
得分: 0
实体不应该在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;
}
英文:
Entities are not supposed to be exposed in the API layer,
You should create a dedicated DTO classes instead.
For example-
@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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论