首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Swagger2 生成API文档时泛型总是显示不出来的问题,解决了

Swagger2 生成API文档时泛型总是显示不出来的问题,解决了

作者头像
Java架构师必看
发布2021-10-18 18:02:47
发布2021-10-18 18:02:47
3.3K00
代码可运行
举报
文章被收录于专栏:Java架构师必看Java架构师必看
运行总次数:0
代码可运行

Swagger2 生成API文档时泛型总是显示不出来的问题

强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说Swagger2 生成API文档时泛型总是显示不出来的问题,解决了,希望能够帮助大家进步!!!

## 接口响应消息体

代码语言:javascript
代码运行次数:0
运行
复制
/* * * Copyright (c) 2018-2025, lengleng All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the hzcloud.com developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: lengleng (wangiegie@gmail.com) * */package com.hzcloud.hz.common.core.util;import com.hzcloud.hz.common.core.constant.CommonConstants;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.*;import lombok.experimental.Accessors;import java.io.Serializable;/** * 响应信息主体 * * @param <T> * @author lengleng */@ToString@NoArgsConstructor@AllArgsConstructor@Accessors(chain = true)@ApiModel(value = "响应信息主体")public class R<T> implements Serializable { private static final long serialVersionUID = 1L;@Getter@Setter@ApiModelProperty(value = "返回标记:成功标记=0,失败标记=1")private int code;@Getter@Setter@ApiModelProperty(value = "返回信息")private String msg;@Getter@Setter@ApiModelProperty(value = "数据")private T data;public static <T> R<T> ok() { return restResult(null, CommonConstants.SUCCESS, null);}public static <T> R<T> ok(T data) { return restResult(data, CommonConstants.SUCCESS, null);}public static <T> R<T> ok(T data, String msg) { return restResult(data, CommonConstants.SUCCESS, msg);}public static <T> R<T> failed() { return restResult(null, CommonConstants.FAIL, null);}public static <T> R<T> failed(String msg) { return restResult(null, CommonConstants.FAIL, msg);}public static <T> R<T> failed(T data) { return restResult(data, CommonConstants.FAIL, null);}public static <T> R<T> failed(T data, String msg) { return restResult(data, CommonConstants.FAIL, msg);}private static <T> R<T> restResult(T data, int code, String msg) { R<T> apiResult = new R<>();apiResult.setCode(code);apiResult.setData(data);apiResult.setMsg(msg);return apiResult;}}

## 使用:在接口返回值后加上类的名称

#如下返回值为 R 这个Integer就是类型名称

代码语言:javascript
代码运行次数:0
运行
复制
/** * 查询未完成的场次的数量 * @return 未完成的场次的数量 */@GetMapping("/queryUnfinishedImplementationCount")@ApiOperation(value = "查询未完成的场次的数量", notes = "查询未完成的场次的数量")public R<Integer> getById() { QueryWrapper<FlightplanSet> flightplanSetQueryWrapper=new QueryWrapper<>();flightplanSetQueryWrapper.eq("implementation_status",Constants.FLIGHT_PLAN_SET_IMPLEMENTATION_STATUS_OF_UNFINISHED);flightplanSetQueryWrapper.eq("del_flag",Constants.DEL_FLAG_NORMAL);return R.ok(flightplanSetService.count(flightplanSetQueryWrapper));}

结果,swagger就会生成带有返回结构的响应体

猜您喜欢:
  1. Spring Security OAuth2 授权失败(401),解决了
  2. The valid characters are defined in RFC 7230 and RFC 3986问题,解决了,特此记录
  3. mybaties :required string parameter ‘XXX‘is not present,解决了
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ## 接口响应消息体
  • ## 使用:在接口返回值后加上类的名称
  • 结果,swagger就会生成带有返回结构的响应体
    • 猜您喜欢:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档