当前位置:   article > 正文

swagger设置字段required必填_swagger required

swagger required

swagger注解中需要设置请求参数的字段是必须填写的还是非必须的。我们需要添加下面的配置。只需要在对象的字段中增加下面的代码

@ApiModelProperty(value = "自建应用的corpid", required = true)

显示效果如下

        

详细的代码如下

请求的controller代码

  1. package my.controller;
  2. import my.bean.*;
  3. import my.common.BaseController;
  4. import my.configuration.MyRedissonConfig;
  5. import my.service.*;
  6. import my.util.MsgUtil;
  7. import my.util.MyConstants;
  8. import my.util.WxUtil;
  9. import my.wechataes.WXBizMsgCrypt;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import io.swagger.annotations.ApiResponse;
  13. import io.swagger.annotations.ApiResponses;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.scheduling.annotation.EnableAsync;
  17. import org.springframework.validation.annotation.Validated;
  18. import org.springframework.web.bind.annotation.*;
  19. import springfox.documentation.annotations.ApiIgnore;
  20. import javax.annotation.Resource;
  21. import javax.servlet.ServletInputStream;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.BufferedReader;
  25. import java.io.InputStreamReader;
  26. import java.util.Map;
  27. @EnableAsync
  28. @RestController
  29. @Api(value = "MyAppController",tags = {"自建应用服务"})
  30. @RequestMapping("/myApp")
  31. @Slf4j
  32. public class MyAppController extends BaseController {
  33. @Autowired
  34. private MyAppService myAppService;
  35. @ApiOperation(value = "自建应用-推送图文消息给用户",
  36. notes = "逻辑如下:"+ MyConstants.SWAGGER2_FOX_BR_TAG +
  37. MyConstants.SWAGGER2_FOX_BR_TAG)
  38. @RequestMapping(value = "/sendNews", method = RequestMethod.POST)
  39. @ResponseBody
  40. public String sendNews(@Validated @RequestBody MyAppSendNewsReqBean vo) {
  41. String code = MyConstants.SUCCESS;
  42. try {
  43. log.info("myApp sendNews vo={}",vo);
  44. myAppService.sendNews(vo);
  45. } catch (Exception e) {
  46. code = MyConstants.FAILURE;
  47. e.printStackTrace();
  48. }
  49. return MsgUtil.outJson(code);
  50. }
  51. }

请求参数对象

  1. package my.bean;
  2. import io.swagger.annotations.ApiModel;
  3. import io.swagger.annotations.ApiModelProperty;
  4. import lombok.AllArgsConstructor;
  5. import lombok.Builder;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. import lombok.experimental.Accessors;
  9. import lombok.extern.slf4j.Slf4j;
  10. import javax.validation.constraints.NotNull;
  11. import java.io.Serializable;
  12. @ApiModel(
  13. description = "图文消息对象"
  14. )
  15. @Data
  16. @Slf4j
  17. @Builder
  18. @NoArgsConstructor
  19. @AllArgsConstructor
  20. @Accessors(chain = true)
  21. public class MyAppSendNewsReqBean implements Serializable {
  22. @ApiModelProperty(value = "企微自建应用,接收消息的用户ID,多个接收者用‘|’分隔,最多支持1000个)。@all=全员发送", required = true)
  23. @NotNull(message = "userId不能为空")
  24. private String userId;
  25. @ApiModelProperty(value = "自建应用的corpid", required = true)
  26. @NotNull(message = "corpid不能为空")
  27. private String corpid;
  28. @ApiModelProperty(value = "自建应用的corpsecret", required = true)
  29. @NotNull(message = "corpsecret不能为空")
  30. private String corpsecret;
  31. @ApiModelProperty(value = "自建应用的agentId", required = true)
  32. @NotNull(message = "agentId不能为空")
  33. private Integer agentId;
  34. @ApiModelProperty(value = "图文消息的标题", required = true)
  35. @NotNull(message = "title不能为空")
  36. private String title;
  37. @ApiModelProperty(value = "图文消息的描述")
  38. private String description;
  39. @ApiModelProperty(value = "点击图片跳转的网址,网址必须是备案的网址,否则会有风险提示")
  40. private String url;
  41. @ApiModelProperty(value = "图片地址", required = true)
  42. @NotNull(message = "picUrl不能为空")
  43. private String picUrl;
  44. }

启动项目,访问swagger,访问地址如下

  1. log.info("swagger api=http://ip:port/项目名称/swagger-ui.html");
  2. log.info("swagger api=http://ip:port/项目名称/doc.html");

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/373076
推荐阅读
相关标签
  

闽ICP备14008679号