代码拉取完成,页面将自动刷新
依赖版本:
<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;
}
}
文档无响应参数显示:
把
@ApiModel(description="接口返回结果")
去掉
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
无效!
另外,我改写了一个结果类,是可以显示的:
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("通用接口返回对象")
public class Results<T> {
@ApiModelProperty(required = true,notes = "结果码",example = "200")
private int state;
@ApiModelProperty(required = true,notes = "时间戳",example = "1567425139000")
private long time;
@ApiModelProperty(required = true,notes = "返回信息",example = "SUCCESS")
private String message;
@ApiModelProperty(required = true,notes = "返回数据",example = "{\"name\":\"blues\"}")
private T content;
public Results(int code, String msg, T obj){
setState(code);
setMessage(msg);
setContent(obj);
setTime(System.currentTimeMillis());
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getContent() {
return content;
}
public void setContent(T content) {
this.content = content;
}
}
用我刚刚提供的写法是不能显示的,好诡异
我想我找到问题所在了,要写成这样才行:
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
而不能写成这样:
public int state() {
return state;
}
public void state(int state) {
this.state = state;
}
希望在后续的版本中可以支持!!!
我想我找到问题所在了,要写成这样才行:
public int getState() { return state; } public void setState(int state) { this.state = state; }
而不能写成这样:
public int state() { return state; } public void state(int state) { this.state = state; }
希望在后续的版本中可以支持!!!
@LeeFJ 这个不是ui控制的,解析部分springfox来识别,如果最终识别代码没有转成OpenAPI的结构,那Knife4j的ui也不会显示,Ui只认OpenAPI结构
好的,了解了。把 get 方法加上就行了。不过从APi设计角度看,有些别扭,也只能这样了。
登录 后才可以发表评论