swagger如何返回map字段注释

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

效果

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

(0)

相关推荐

  • django-rest-swagger对API接口注释的方法

    Swagger是一个API开发者的工具框架,用于生成.描述.调用和可视化RESTful风格的Web服务.总体目标是使客户端和文件系统服务器以同样的速度来更新,方法,参数和模型紧密集成到服务器端的代码中,允许API始终保持同步. 在使用 django-rest-framework 进行API开发,可以使用django-rest-swagger接入swagger自动生成接口文档. 1. 安装django-rest-swagger pip install django-rest-swagger 2.配

  • 解决Swagger2返回map复杂结构不能解析的问题

    今天有同事用swagger2开发时,有一方法返回Map<String,List<Object>>出现无法解析错误. Pom.xml引入的swagger版本如下: <!--swagger start--> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.

  • Spring Boot整合Swagger2的完整步骤详解

    前言 swagger,中文"拽"的意思.它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅, 而且还提供了在线文档的测试.另外swagger很容易构建restful风格的api. 一.Swagger概述 Swagger是一组围绕OpenAPI规范构建的开源工具,可帮助设计.构建.记录和使用REST API. 简单说下,它的出现就是为了方便进行测试后台的restful形式的接口,实现动态的更新,当我们在后台的接口 修改了后,swagger可以实现自动的更新,而不需要

  • swagger如何返回map字段注释

    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; i

  • 解决mybatis用Map返回的字段全变大写的问题

    mybatis通常情况都是用javabean作为resultType的对象,但是有时也可以使用Map去接收. <select id="execute" parameterType="String" resultType="java.util.HashMap"> ${value} </select> 如果使用Map,返回来的字段名全是大写,处理方法 Select name as "name" from v

  • mybatis返回map类型数据空值字段不显示的解决方案

    目录 mybatis返回map数据空值字段不显示 查询sql添加每个字段的判断空 ResultType利用实体返回 springMVC+mybatis查询数据 mybatis返回map空值未返回字段 mybatis开启CallSettersOnNulls mybatis返回map数据空值字段不显示 查询sql添加每个字段的判断空 IFNULL(rate,'') as rate ResultType利用实体返回 不用map springMVC+mybatis查询数据 返回resultType=”m

  • ThinkPHP查询返回简单字段数组的方法

    本文实例讲述了ThinkPHP查询返回简单字段数组的方法,是ThinkPHP程序设计中一个很实用的功能.具体方法如下: 通常来说使用select语句.返回的都是结构较复杂的字段数组.如以下是一个简单的查询: $map['parentid'] = $id; $sub_ids = D('Category')->where($map)->field("catid")->select(); 查询后,得到的结果是: [{"catid":"23&qu

  • 让JPA的Query查询接口返回Map对象的方法

    在JPA 2.0 中我们可以使用entityManager.createNativeQuery()来执行原生的SQL语句. 但当我们查询结果没有对应实体类时,query.getResultList()返回的是一个List<Object[]>.也就是说每行的数据被作为一个对象数组返回. 常见的用法是这样的: public void testNativeQuery(){ Query query = entityManager.createNativeQuery("select id, n

  • mybatis-plus返回map自动转驼峰配置操作

    mybatis-plus返回map自动转驼峰配置object-wrapper-factory不生效问题解决:配置map-underscore-to-camel-case: true不生效问题解决 很多时候我们工作中查询很多字段的时候一般是返回一个VO来接收,这个时候我们只要在yml中配置了 map-underscore-to-camel-case: true 就会自动将查询数据库的字段带下划线的属性转成对应实体类VO中驼峰命名的属性. 但是会经常有这种场景:例如我们只查询2个字段要返回给前端,这

  • mybatis 返回Map类型key改为小写的操作

    默认情况下,当resultType="java.util.Map"时,返回的key值都是大写的. 现在想key改成自己想要的,只需为查询出来的字段增加个别名即可. 如: <select id="getStudentList" resultType="java.util.Map"> select t.name as "sName",t.sex as "sSex" from student <

  • Mybatis查找返回Map,List集合类型的数据方式

    Mybatis查找返回Map,List集合类型的数据 一.查找返回Bean对象的List集合 基本与返回Bean对象没什么区别,resultType依然为Bean对象的全类名,只是接口中的方法类型要进行修改 public List<Employee> getEmpListByEmail(String email); 二.查询返回Bean对象的Map集合 同样,resultType依然为Bean对象的全类名,只是接口中的方法类型要进行修改,添加注解. @MapKey("Bean对象属性

  • mybatis返回map结果集@MapKey使用的场景分析

    目录 mybatis返回map结果集@MapKey使用场景 使用id作为map的ke Map的value为Map,一条记录对应一个Map 使用name作为map的key mybatis使用@MapKey注解 背景和含义 具体示例 mybatis返回map结果集@MapKey使用场景 select的 resultType属性为map时: 通过MapKey指定map的key值 使用id作为map的ke @MapKey("id") Map<Long, UserInfo> getU

随机推荐