【问题标题】:required for input parameters on swaggerswagger 上的输入参数需要
【发布时间】:2019-03-04 15:52:17
【问题描述】:

当我在休息时使用@RequestParamrequired = true 并且在大摇大摆地测试它时,它会与旁边的 *required 标记一起显示。

@GetMapping(path = "/getinfo")
    public ResponseEntity<?> getMyInfo(@RequestParam(value = "input", required = true)  int input, other request parameters)

但是现在,如果我使用 @ModelAttribute 将 url 映射到对象,我该如何在 swagger 上实现同样的目标。

@GetMapping(path = "/getinfo")
        public ResponseEntity<?> getMyInfo(@ModelAttribute MyObject myObject)

【问题讨论】:

    标签: rest spring-boot swagger swagger-ui swagger-2.0


    【解决方案1】:

    您可以尝试使用注解@ApiParam

    @GetMapping(path = "/getinfo")
    public ResponseEntity<?> getMyInfo(@ModelAttribute("myObject") MyObject myObject)
    

    在你的 MyObject 类中

    public class MyObject {
    
      private long id;
    
      @ApiParam(name = "name", value = "Name is Mandatory", required = true)   
      private String name;
    
    }
    

    现在,name 将成为 *必填 字段。

    【讨论】:

    • 但并非 MyObject 中的所有属性都是必需的。我只想对某些属性设置 required
    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 2015-04-17
    • 2017-05-08
    • 2015-05-19
    • 1970-01-01
    • 2020-10-02
    • 2018-04-30
    • 2016-05-30
    相关资源
    最近更新 更多