swagger如何返回map字段注释

时间:2022-09-12 13:50:38

1.效果图如下:

swagger如何返回map字段注释

2.controller层代码:

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.mengyoou.core.serialize.ResponseMsg;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import io.swagger.annotations.ApiResponse;
  11. import io.swagger.annotations.ApiResponses;
  12.  
  13. @Api(value="返回字段添加注释信息controller",tags={"返回字段添加注释信息controller"})
  14. @RestController
  15. public class TestController {
  16. @ApiOperation(value="返回字段添加注释信息", notes="返回字段添加注释信息")
  17. @RequestMapping(value={"demo"}, method={RequestMethod.GET})
  18. @ApiResponses({
  19. @ApiResponse(code = 200, message = "ok", response=User.class),
  20. })
  21. public ResponseMsg demo() {
  22. User user = new User();
  23. Map<String, Object> map = new HashMap<>();
  24. map.put("user", user);
  25. return new ResponseMsg(map);
  26. }
  27. }

3.用户实体的代码:

  1. import io.swagger.annotations.ApiModel;
  2. import io.swagger.annotations.ApiModelProperty;
  3. import lombok.Data;
  4. @ApiModel(value="登录成功后返回的个人信息")
  5. @Data
  6. public class User {
  7.  
  8. @ApiModelProperty(value="用户名")
  9. private String userName;//用户名
  10. @ApiModelProperty(value="密码")
  11. private String password;//用户名
  12. }

4.关键点:

swagger如何返回map字段注释

接口 Swagger 显示返回模型的注释

mark:环境看之前文章

目的:web api controller 调用 asp.net mvc controller,让swagger里面的返回模型支持注释

关键:对返回消息类的封装,返回数据为泛型,swagger就能显示model的注释了

JsonMsg

  1. /// <summary>
  2. /// 返回消息
  3. /// </summary>
  4. public class JsonMsg<T> where T : class
  5. {
  6. /// <summary>
  7. /// 状态码
  8. /// </summary>
  9. public int code { get; set; }
  10. /// <summary>
  11. /// 消息
  12. /// </summary>
  13. public string msg { get; set; }
  14. /// <summary>
  15. /// 内容
  16. /// </summary>
  17. public T obj { get; set; }
  18. /// <summary>
  19. /// 图标
  20. /// </summary>
  21. public int icon { get; set; }
  22. public static JsonMsg<T> OK(T obj, string msg = "成功")
  23. {
  24. return new JsonMsg<T>() { code = 1, msg = msg, obj = obj, icon = 1 };
  25. }
  26. public static JsonMsg<T> Error(T obj, string msg = "失败")
  27. {
  28. return new JsonMsg<T>() { code = 0, msg = msg, obj = obj, icon = 1 };
  29. }
  30. }

OrderDto

  1. public class OrderDto
  2. {
  3. public string Name { get; set; }
  4. }

HomeController

  1. public JsonResult GetOrderInfo()
  2. {
  3. var order = new OrderDto { Name = "203022200" };
  4. return Json(order);
  5. }

HomeAPIController

  1. [AllowAnonymous]
  2. public JsonMsg<OrderDto> testResultDataDesc3()
  3. {
  4. HomeController controller = GetController<HomeController>();
  5. var d = (OrderDto)controller.GetOrderInfo().Data;
  6. return JsonMsg<OrderDto>.OK(d);
  7. }

效果

swagger如何返回map字段注释

swagger如何返回map字段注释

swagger如何返回map字段注释

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/xingsfdz/article/details/105919031