登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
轻量养虾,开箱即用!低 Token + 稳定算力,Gitee & 模力方舟联合出品的 PocketClaw 正式开售!点击了解详情
代码拉取完成,页面将自动刷新
开源项目
>
开发工具
>
开发/调试
&&
WEB应用开发
>
WebUI组件/框架
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
790
Star
7K
Fork
1.5K
GVP
萧明
/
knife4j
代码
Issues
248
Pull Requests
2
Wiki
统计
流水线
服务
JavaDoc
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
开发画像分析
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
响应参数不显示
已完成
#I2CY7K
LeeFJ
创建于
2021-01-12 16:06
依赖版本: ``` <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.9.RELEASE</version> </parent> ``` ``` <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-micro-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> ``` ``` spring-cloud-dependencies 版本 Hoxton.RELEASE alibaba 微服务组件版本 2.2.2.RELEASE ``` 使用Nacos作为注册中心,集中接口数据展示。 配置类: ``` package com.chinayie.platform.common.knife4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.bind.annotation.RestController; import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Swagger API相关配置 */ @Configuration @EnableSwagger2 @EnableKnife4j public class Swagger2Config { @Value("${knife4j.title}") private String title; @Value("${knife4j.version}") private String version; @Value("${knife4j.description}") private String description; @Bean public Docket createRestApi(){ Docket docket=new Docket(DocumentationType.SWAGGER_2) //不使用默认的响应码 .useDefaultResponseMessages(false) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build(); return docket; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(title).description(description).version(version) .build(); } } ``` 控制器: ``` package com.chinayie.platform.example.controller; import java.math.BigDecimal; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.chinayie.platform.agent.service.example.MenuServiceAgent; import com.chinayie.platform.common.sentinel.SentinelExceptionUtil; import com.chinayie.platform.common.web.BaseController; import com.chinayie.platform.common.web.rest.Result; import com.chinayie.platform.domain.example.po.Menu; import com.chinayie.platform.domain.example.vo.MenuVO; import com.chinayie.platform.example.service.IMenuService; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; /** * @author 李方捷 * @since 2021-01-08 05:31:28 */ @Api(value = "菜单") @RestController public class MenuController extends BaseController { @Autowired private IMenuService menuService; /** * 按主键获取菜单 */ @ApiOperation(value = "按主键获取菜单",response=Menu.class) @ApiOperationSupport(author = "xiaoymin@foxmail.com") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "编号" , required = true , dataTypeClass=Long.class,example = "123") }) @SentinelResource(value = MenuServiceAgent.GET_BY_ID, blockHandlerClass = { SentinelExceptionUtil.class },blockHandler = SentinelExceptionUtil.HANDLER) @PostMapping(MenuServiceAgent.GET_BY_ID) public Result<Menu> selectById(Long id) { Result<Menu> result=new Result<>(); Menu menu=menuService.selectById(id); result.success(true).data(menu); return result; } /** * 查询菜单 */ @ApiOperation(value = "查询菜单",response=Menu.class) @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "编号" , required = false , dataTypeClass=Long.class), @ApiImplicitParam(name = "shopId",value = "店铺ID" , required = false , dataTypeClass=Long.class), @ApiImplicitParam(name = "name",value = "店名" , required = false , dataTypeClass=String.class), @ApiImplicitParam(name = "price",value = "单价" , required = false , dataTypeClass=BigDecimal.class), @ApiImplicitParam(name = "photo",value = "图片地址" , required = false , dataTypeClass=String.class), }) @SentinelResource(value = MenuServiceAgent.SELECT_LIST, blockHandlerClass = { SentinelExceptionUtil.class },blockHandler = SentinelExceptionUtil.HANDLER) @PostMapping(MenuServiceAgent.SELECT_LIST) public Result<List<Menu>> selectList(MenuVO sample) { Result<List<Menu>> result=new Result<>(); List<Menu> list=menuService.selectList(sample); result.success(true).data(list); return result; } /** * 添加菜单 */ @ApiOperation(value = "添加菜单") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "编号" , required = true , dataTypeClass=Long.class), @ApiImplicitParam(name = "shopId",value = "店铺ID" , required = false , dataTypeClass=Long.class), @ApiImplicitParam(name = "name",value = "店名" , required = true , dataTypeClass=String.class), @ApiImplicitParam(name = "price",value = "单价" , required = false , dataTypeClass=BigDecimal.class), @ApiImplicitParam(name = "photo",value = "图片地址" , required = false , dataTypeClass=String.class), }) @SentinelResource(value = MenuServiceAgent.INSERT, blockHandlerClass = { SentinelExceptionUtil.class },blockHandler = SentinelExceptionUtil.HANDLER) @PostMapping(MenuServiceAgent.INSERT) public Result<Menu> insert(MenuVO menuVO) { Result<Menu> result=new Result<>(); int i=menuService.insert(menuVO); result.success(i>0).data(menuVO); return result; } } ``` 结果类: ``` package com.chinayie.platform.common.web.rest; import java.io.Serializable; import java.util.Collection; import java.util.Map; import com.alibaba.fastjson.JSON; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(description="接口返回结果") public class Result<T> implements Serializable { private static final long serialVersionUID = -1902733018654069374L; /** * 默认为成功 * */ public Result() { this(true); } public Result(boolean success) { this.success(success); } private T data; private String dataType; private String componentType; public Result<T> data(T data) { this.data = data; if(this.data!=null) { this.dataType=this.data.getClass().getName(); //识别元素类型 if(this.data.getClass().isArray()) { this.componentType=this.data.getClass().getComponentType().getName(); } else if (this.data instanceof Collection) { Collection coll=(Collection)this.data; if(!coll.isEmpty()) { Object el=null; while(el==null) { el=coll.iterator().next(); } if(el!=null) { this.componentType=el.getClass().getName(); } } } } else { this.dataType=null; } return this; } public T data() { return data; } private boolean success = true; @ApiModelProperty(required = true,notes = "结果码",example = "OK") private String code; public String code() { return code; } public Result<T> code(String code) { this.code = code; return this; } @ApiModelProperty(notes = "提示信息") private String message; public boolean success() { return success; } @SuppressWarnings("rawtypes") public Result<T> copyAsError(Result source) { return this.success(false).code(source.code()).message(source.message()); } public Result<T> success(boolean success) { this.success = success; return this; } public String message() { return message; } public Result<T> message(String message) { this.message = message; return this; } public <E> E getEntity(Class<E> type) { if(this.data == null) return null; E e=JSON.parseObject(JSON.toJSONString(this.data), type); return e; } public <E> E getEntity(Object key, Class<E> type) { if(this.data == null) return null; if(!(this.data instanceof Map)) { throw new IllegalArgumentException("data is not type of Map"); } Map map=(Map)this.data; Object keyData=map.get(key); E e=JSON.parseObject(JSON.toJSONString(keyData), type); return e; } public String dataType() { return dataType; } public String componentType() { return componentType; } } ``` 文档无响应参数显示: 
依赖版本: ``` <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.9.RELEASE</version> </parent> ``` ``` <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-micro-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> ``` ``` spring-cloud-dependencies 版本 Hoxton.RELEASE alibaba 微服务组件版本 2.2.2.RELEASE ``` 使用Nacos作为注册中心,集中接口数据展示。 配置类: ``` package com.chinayie.platform.common.knife4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.bind.annotation.RestController; import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Swagger API相关配置 */ @Configuration @EnableSwagger2 @EnableKnife4j public class Swagger2Config { @Value("${knife4j.title}") private String title; @Value("${knife4j.version}") private String version; @Value("${knife4j.description}") private String description; @Bean public Docket createRestApi(){ Docket docket=new Docket(DocumentationType.SWAGGER_2) //不使用默认的响应码 .useDefaultResponseMessages(false) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build(); return docket; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(title).description(description).version(version) .build(); } } ``` 控制器: ``` package com.chinayie.platform.example.controller; import java.math.BigDecimal; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.chinayie.platform.agent.service.example.MenuServiceAgent; import com.chinayie.platform.common.sentinel.SentinelExceptionUtil; import com.chinayie.platform.common.web.BaseController; import com.chinayie.platform.common.web.rest.Result; import com.chinayie.platform.domain.example.po.Menu; import com.chinayie.platform.domain.example.vo.MenuVO; import com.chinayie.platform.example.service.IMenuService; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; /** * @author 李方捷 * @since 2021-01-08 05:31:28 */ @Api(value = "菜单") @RestController public class MenuController extends BaseController { @Autowired private IMenuService menuService; /** * 按主键获取菜单 */ @ApiOperation(value = "按主键获取菜单",response=Menu.class) @ApiOperationSupport(author = "xiaoymin@foxmail.com") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "编号" , required = true , dataTypeClass=Long.class,example = "123") }) @SentinelResource(value = MenuServiceAgent.GET_BY_ID, blockHandlerClass = { SentinelExceptionUtil.class },blockHandler = SentinelExceptionUtil.HANDLER) @PostMapping(MenuServiceAgent.GET_BY_ID) public Result<Menu> selectById(Long id) { Result<Menu> result=new Result<>(); Menu menu=menuService.selectById(id); result.success(true).data(menu); return result; } /** * 查询菜单 */ @ApiOperation(value = "查询菜单",response=Menu.class) @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "编号" , required = false , dataTypeClass=Long.class), @ApiImplicitParam(name = "shopId",value = "店铺ID" , required = false , dataTypeClass=Long.class), @ApiImplicitParam(name = "name",value = "店名" , required = false , dataTypeClass=String.class), @ApiImplicitParam(name = "price",value = "单价" , required = false , dataTypeClass=BigDecimal.class), @ApiImplicitParam(name = "photo",value = "图片地址" , required = false , dataTypeClass=String.class), }) @SentinelResource(value = MenuServiceAgent.SELECT_LIST, blockHandlerClass = { SentinelExceptionUtil.class },blockHandler = SentinelExceptionUtil.HANDLER) @PostMapping(MenuServiceAgent.SELECT_LIST) public Result<List<Menu>> selectList(MenuVO sample) { Result<List<Menu>> result=new Result<>(); List<Menu> list=menuService.selectList(sample); result.success(true).data(list); return result; } /** * 添加菜单 */ @ApiOperation(value = "添加菜单") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "编号" , required = true , dataTypeClass=Long.class), @ApiImplicitParam(name = "shopId",value = "店铺ID" , required = false , dataTypeClass=Long.class), @ApiImplicitParam(name = "name",value = "店名" , required = true , dataTypeClass=String.class), @ApiImplicitParam(name = "price",value = "单价" , required = false , dataTypeClass=BigDecimal.class), @ApiImplicitParam(name = "photo",value = "图片地址" , required = false , dataTypeClass=String.class), }) @SentinelResource(value = MenuServiceAgent.INSERT, blockHandlerClass = { SentinelExceptionUtil.class },blockHandler = SentinelExceptionUtil.HANDLER) @PostMapping(MenuServiceAgent.INSERT) public Result<Menu> insert(MenuVO menuVO) { Result<Menu> result=new Result<>(); int i=menuService.insert(menuVO); result.success(i>0).data(menuVO); return result; } } ``` 结果类: ``` package com.chinayie.platform.common.web.rest; import java.io.Serializable; import java.util.Collection; import java.util.Map; import com.alibaba.fastjson.JSON; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(description="接口返回结果") public class Result<T> implements Serializable { private static final long serialVersionUID = -1902733018654069374L; /** * 默认为成功 * */ public Result() { this(true); } public Result(boolean success) { this.success(success); } private T data; private String dataType; private String componentType; public Result<T> data(T data) { this.data = data; if(this.data!=null) { this.dataType=this.data.getClass().getName(); //识别元素类型 if(this.data.getClass().isArray()) { this.componentType=this.data.getClass().getComponentType().getName(); } else if (this.data instanceof Collection) { Collection coll=(Collection)this.data; if(!coll.isEmpty()) { Object el=null; while(el==null) { el=coll.iterator().next(); } if(el!=null) { this.componentType=el.getClass().getName(); } } } } else { this.dataType=null; } return this; } public T data() { return data; } private boolean success = true; @ApiModelProperty(required = true,notes = "结果码",example = "OK") private String code; public String code() { return code; } public Result<T> code(String code) { this.code = code; return this; } @ApiModelProperty(notes = "提示信息") private String message; public boolean success() { return success; } @SuppressWarnings("rawtypes") public Result<T> copyAsError(Result source) { return this.success(false).code(source.code()).message(source.message()); } public Result<T> success(boolean success) { this.success = success; return this; } public String message() { return message; } public Result<T> message(String message) { this.message = message; return this; } public <E> E getEntity(Class<E> type) { if(this.data == null) return null; E e=JSON.parseObject(JSON.toJSONString(this.data), type); return e; } public <E> E getEntity(Object key, Class<E> type) { if(this.data == null) return null; if(!(this.data instanceof Map)) { throw new IllegalArgumentException("data is not type of Map"); } Map map=(Map)this.data; Object keyData=map.get(key); E e=JSON.parseObject(JSON.toJSONString(keyData), type); return e; } public String dataType() { return dataType; } public String componentType() { return componentType; } } ``` 文档无响应参数显示: 
评论 (
5
)
登录
后才可以发表评论
状态
已完成
待办的
进行中
已完成
已关闭
负责人
未设置
标签
question
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (
-
)
标签 (
-
)
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(2)
Java
1
https://gitee.com/xiaoym/knife4j.git
git@gitee.com:xiaoym/knife4j.git
xiaoym
knife4j
knife4j
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册