如何根据请求类型动态地为Spring Boot中的Swagger请求模型定义参数列表。

huangapple go评论87阅读模式
英文:

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(&quot;/api&quot;)
public class BatchController extends ControllerConfig {

    @PostMapping(&quot;/batch&quot;)
    public GeneralResponse&lt;Boolean&gt; createBatch(@RequestBody Batch batch) throws Exception{
        try{
            batchService.createBatch(batch);
            return new GeneralResponse&lt;&gt;(true,&quot;batch created successfully&quot;, true, System.currentTimeMillis(), HttpStatus.OK);
        } catch (Exception e){

            return new GeneralResponse&lt;&gt;(false,e.getMessage(), false, System.currentTimeMillis(), HttpStatus.BAD_REQUEST);
        }
    }


    @PutMapping(&quot;/batch&quot;)
    public GeneralResponse&lt;Boolean&gt; updateBatch(@RequestBody Batch batch) {
        try {
            batchService.updateBatch(batch);
            return new GeneralResponse&lt;&gt;(true, &quot;batch updated successfully&quot;, true, System.currentTimeMillis(), HttpStatus.OK);
        } catch (Exception e) {
            return new GeneralResponse&lt;&gt;(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: &quot;2020-10-04T21:18:00.656Z&quot;,
    remark: &quot;string&quot;
}

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;
}

huangapple
  • 本文由 发表于 2020年10月5日 05:32:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64200112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定