使用Knife4j加强实现的Swagger生成API文档

This commit is contained in:
zyx 2023-02-28 18:49:53 +08:00
parent d75037fe93
commit 529a0a755c
3 changed files with 39 additions and 4 deletions

View File

@ -37,6 +37,11 @@
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -1,11 +1,20 @@
package cn.czyx007.reggie.config;
import cn.czyx007.reggie.common.JacksonObjectMapper;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;
import java.util.List;
@ -14,13 +23,36 @@ import java.util.List;
* @createTime : 2022/12/20 - 19:54
*/
@Configuration
@EnableSwagger2
@EnableKnife4j
public class WebMvcConfig extends WebMvcConfigurationSupport {
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("瑞吉外卖")
.version("1.0")
.description("瑞吉外卖接口文档")
.build();
}
@Bean
public Docket createRestApi(){
//文档类型
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.czyx007.reggie.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 设置静态资源映射
* @param registry
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/static/backend/");
registry.addResourceHandler("/front/**").addResourceLocations("classpath:/static/front/");
}

View File

@ -1,10 +1,8 @@
package cn.czyx007.reggie.filter;
import cn.czyx007.reggie.bean.Employee;
import cn.czyx007.reggie.common.BaseContext;
import cn.czyx007.reggie.common.R;
import com.alibaba.fastjson.JSON;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.util.AntPathMatcher;
import javax.servlet.*;
@ -21,8 +19,8 @@ import java.io.IOException;
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
public static final String[] urls = {"/employee/login", "/employee/logout", "/backend/**", "/front/**",
"/user/sendMsg", "/user/login"};
private static final String[] urls = {"/employee/login", "/employee/logout", "/backend/**", "/front/**",
"/user/sendMsg", "/user/login", "/doc.html", "/webjars/**", "/swagger-resources", "/v2/api-docs"};
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {