1.效果图如下:
2.controller层代码:
import java.util.hashmap; import java.util.map; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; import com.mengyoou.core.serialize.responsemsg; import io.swagger.annotations.api; import io.swagger.annotations.apioperation; import io.swagger.annotations.apiresponse; import io.swagger.annotations.apiresponses; @api(value="返回字段添加注释信息controller",tags={"返回字段添加注释信息controller"}) @restcontroller public class testcontroller { @apioperation(value="返回字段添加注释信息", notes="返回字段添加注释信息") @requestmapping(value={"demo"}, method={requestmethod.get}) @apiresponses({ @apiresponse(code = 200, message = "ok", response=user.class), }) public responsemsg demo() { user user = new user(); map<string, object> map = new hashmap<>(); map.put("user", user); return new responsemsg(map); } }
3.用户实体的代码:
import io.swagger.annotations.apimodel; import io.swagger.annotations.apimodelproperty; import lombok.data; @apimodel(value="登录成功后返回的个人信息") @data public class user { @apimodelproperty(value="用户名") private string username;//用户名 @apimodelproperty(value="密码") private string password;//用户名 }
4.关键点:
接口 swagger 显示返回模型的注释
mark:环境看之前文章
目的:web api controller 调用 asp.net mvc controller,让swagger里面的返回模型支持注释
关键:对返回消息类的封装,返回数据为泛型,swagger就能显示model的注释了
jsonmsg
/// <summary> /// 返回消息 /// </summary> public class jsonmsg<t> where t : class { /// <summary> /// 状态码 /// </summary> public int code { get; set; } /// <summary> /// 消息 /// </summary> public string msg { get; set; } /// <summary> /// 内容 /// </summary> public t obj { get; set; } /// <summary> /// 图标 /// </summary> public int icon { get; set; } public static jsonmsg<t> ok(t obj, string msg = "成功") { return new jsonmsg<t>() { code = 1, msg = msg, obj = obj, icon = 1 }; } public static jsonmsg<t> error(t obj, string msg = "失败") { return new jsonmsg<t>() { code = 0, msg = msg, obj = obj, icon = 1 }; } }
orderdto
public class orderdto { public string name { get; set; } }
homecontroller
public jsonresult getorderinfo() { var order = new orderdto { name = "203022200" }; return json(order); }
homeapicontroller
[allowanonymous] public jsonmsg<orderdto> testresultdatadesc3() { homecontroller controller = getcontroller<homecontroller>(); var d = (orderdto)controller.getorderinfo().data; return jsonmsg<orderdto>.ok(d); }
效果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。
需要了解更多c/c++开发分享swagger如何返回map字段注释,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/643993.html