降低springboot版本为2.4.6,为了更好兼容其他框架。

整合knife4j生成接口文档
This commit is contained in:
xiongfeng
2022-07-26 11:53:16 +08:00
parent 18954caa2b
commit 8d020045b7
11 changed files with 73 additions and 76 deletions

View File

@@ -1,7 +1,7 @@
package cn.xf.basedemo.common.model;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
/**
* @program: xf-boot-base

View File

@@ -6,10 +6,8 @@ import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

View File

@@ -1,19 +1,8 @@
package cn.xf.basedemo.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
@@ -23,10 +12,9 @@ import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
@@ -37,45 +25,46 @@ import java.util.List;
* @create: 2022-06-16 16:44
**/
@Configuration
@EnableKnife4j
@EnableSwagger2
public class SpringFoxSwaggerConfig {
@Bean
public Docket createRestApi() {
public Docket docket(Environment environment) {
// 添加接口请求头参数配置 没有的话 可以忽略
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("token")
.description("令牌")
.defaultValue("")
.modelRef(new ModelRef("string"))
.parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否启动swagger 默认启动
.enable(true)
//所在分组
.groupName("base")
.select()
.apis(RequestHandlerSelectors.basePackage("com.didiplus"))
//指定扫描的包路径
.apis(RequestHandlerSelectors.basePackage("cn.xf.basedemo.controller.business"))
.paths(PathSelectors.any())
.build();
.build()
.globalOperationParameters(pars);
}
private ApiInfo apiInfo() {
Contact contact =new Contact("","","");
return new ApiInfoBuilder()
.title("SpringBoot项目 后台服务API接口文档")
.description("使用 knife4j 搭建的后台服务API接口文档")
.termsOfServiceUrl("http://localhost:8088/")
.contact(contact)
.version("1.0.0")
.build();
private ApiInfo apiInfo() {
Contact author = new Contact("reamindertime", "https://blog.csdn.net/qq_39818325?type=blog", "2439534736@qq.com");
return new ApiInfo(
"开箱即用springboot基础项目文档",
"开箱即用springboot基础项目文档",
"1.0",
"",
author,
"",
"",
new ArrayList()
);
}
/**
* 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
**/
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
}
private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
}

View File

@@ -0,0 +1,23 @@
package cn.xf.basedemo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @program: xf-boot-base
* @ClassName WebMvcConfig
* @description: 放行Knife4j请求
* @author: xiongfeng
* @create: 2022-07-26 11:17
**/
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}

View File

@@ -1,4 +1,4 @@
package cn.xf.basedemo.controller;
package cn.xf.basedemo.controller.business;
import cn.xf.basedemo.common.model.LoginUser;
import cn.xf.basedemo.common.model.RetObj;
@@ -6,8 +6,7 @@ import cn.xf.basedemo.interceptor.SessionContext;
import cn.xf.basedemo.model.res.LoginInfoRes;
import cn.xf.basedemo.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -38,7 +37,7 @@ public class UserController {
}
@ApiOperation(value = "用户信息", notes = "用户信息")
@ApiOperationSupport(order = 1)
@ApiOperationSupport(order = 2)
@PostMapping("/info")
public RetObj info(){
LoginUser loginUser = SessionContext.getInstance().get();

View File

@@ -1,8 +1,9 @@
package cn.xf.basedemo.controller;
package cn.xf.basedemo.controller.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @program: xf-boot-base
@@ -34,7 +35,7 @@ public class WebController {
* @Param [url]
* @return java.lang.String
**/
@RequestMapping(value = "/{url}")
@RequestMapping(value = "/{url}", method = RequestMethod.GET)
public String skipUrl(@PathVariable(name = "url") String url) {
return url;
}

View File

@@ -3,10 +3,9 @@ package cn.xf.basedemo.interceptor;
import cn.xf.basedemo.common.model.LoginUser;
import cn.xf.basedemo.common.utils.ApplicationContextUtils;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;

View File

@@ -1,5 +1,7 @@
package cn.xf.basedemo.model.res;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
@@ -10,10 +12,12 @@ import lombok.Data;
* @create: 2022-07-04 11:46
**/
@Data
@ApiModel(value = "登录请求对象")
public class LoginInfoRes {
/**
* 登录密文
*/
@ApiModelProperty(value = "encryptedData", name = "登录密文")
private String encryptedData;
}

View File

@@ -15,10 +15,10 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@@ -65,7 +65,7 @@ public class UserServiceImpl implements UserService {
e.printStackTrace();
return RetObj.error("账号或密码错误");
}
if (StringUtils.isNotBlank(loginInfo.check())) {
if (!StringUtils.isEmpty(loginInfo.check())) {
return RetObj.error(loginInfo.check());
}
//校验登录账号密码