swagger如何返回map字段注释

来自:互联网
时间:2021-07-03
阅读:
免费资源网 - https://freexyz.cn/

1.效果图如下:

swagger如何返回map字段注释

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如何返回map字段注释

接口 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);
}

效果

swagger如何返回map字段注释

swagger如何返回map字段注释

swagger如何返回map字段注释

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

免费资源网 - https://freexyz.cn/
返回顶部
顶部