<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
当我们打开文档时显示knife4j加载错误
打开控制台发现
文档被进行了再封装,导致加载出现了错误
我们在统一处理的地方打上断点
用它的类型或者其他特征为依据对他取消包装即可;
尽量放在常规封装之后,减少无效的调用,。因为业务代码通常直接就在上面返回了;
private static final String[] exclude={
"Swagger2Controller",
"ApiResourceController"
};
// 避免swagger失效
if(Arrays.asList(exclude).contains(methodParameter.getDeclaringClass().getSimpleName())){
return o;
}
顺便解决一波因为拦截器导致favicon.ico加载失败,同时一直走进拦截器刷日志很烦人的问题,。
之前配置为
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/pzh-api/favicon.ico").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
是错的,修改为
//配置拦截器访问静态资源
//registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/static/");
//registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/static/");
修改完成以后发现doc.html找不到页面,解决:
不要给Swagger的doc和js配置,否则会找不到地址,不配置它使用默认的就好了,
之前由于配置的地址错误,直接就报错跳转/error
然后被拦截器拦截这个路径,直接报了无token的错误;然后走了默认路径;
统一封装响应
public class HttpResult {
private boolean flag;
private Integer code;
private String message;
private Object data;
public HttpResult(boolean flag, Integer code, String message) {
this.flag = flag;
this.code = code;
this.message = message;
}
private static<T> HttpResult success(T data){
return new HttpResult(true,2000,"成功",data);
}
private static<T> HttpResult success(){
return new HttpResult(true,2000,"成功");
}
private static<T> HttpResult fail(){
return new HttpResult(false,5000,"失败了");
}
}