一.spring boot整合knife4j的方式。
- 引入依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
- 配置文件
package com.ywxerp.config.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
/*api相关信息*/
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("标题:xxxxx项目API文档")// 页面标题
.description("描述:xxx项目API文档")// 项目描述
.termsOfServiceUrl() // 服务条款网址
.contact(new Contact("xxx","",""))// 作者信息
.license("Apache 3.0")// 许可证
.version("v1.0")// 版本号
.build();
}
/*全部*/
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("全部")
.select()
.apis(RequestHandlerSelectors.basePackage("com.ywxerp.controller"))
.paths(PathSelectors.any())
.build();
}
/*测试模块*/
@Bean
public Docket api_test() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/test/**"))
.build()
.groupName("测试管理")
.pathMapping("/");
}
/*系统模块*/
@Bean
public Docket api_system() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/system/**"))
.build()
.groupName("系统管理")
.pathMapping("/");
}
}
3.若有拦截器需要放开资源文件
package com.ywxerp.config.interceptor;
import com.ywxerp.interceptors.JWTInterceptors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {
@Autowired
JWTInterceptors jwtInterceptors;
//Knife4j 放行接口
String[] EXCLUDE_PATH = new String[]{"/doc.html","/webjars/**","/v2/api-docs","/service-worker.js","/swagger-resources"};
//实现拦截器 要拦截的路径以及不拦截的路径
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册自定义拦截器,添加拦截路径和排除拦截路径
registry.addInterceptor(jwtInterceptors)
.addPathPatterns("/**")
.excludePathPatterns(EXCLUDE_PATH);
}
//配置拦截器访问静态资源
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
二.spring boot采坑
1.yml 文件中节点需要加空格
#jwt相关配置
jwt:
#token-id
tokenid: xxx //注意空格(若需要设置string格式的int类型数据,如 20210719 需设置为'20210719',不然会转换成16进制)
三.BigDecimal比较
decimal的equals方法不仅仅比较值的大小是否相等,首先比较的是scale(scale是bigdecimal的保留小数点位数,比如 new Bigdecimal("1.001"),scale为3),也就是说,不但值得大小要相等,保留位数也要相等,equals才能返回true。
Bigdecimal b = new Bigdecimal("0") 和 Bigdecimal c = new Bigdecimal("0.0"),用equals比较,返回就是false。
Bigdecimal.ZERO的scale为0。
所以,用equals方法要注意这一点。
用b.compareTo(BigDecimal.ZERO)==0,可以比较是否等于0,返回true则等于0,返回false,则不等于0
四.结局反编译项目在idea中初次加载时显示xxx is unsupported binary file,意思是xxx项目是不支持的二进制文件。
1.看项目中是否有.classpash文件,如果有则删除掉该文件即可。