Swagger 404 错误
Swagger 404 错误产生原因:
SpringBoot2.x 版本以后的配置加载问题引起,具体可以去看源码,或者转参考去看,针对这个问题解决办法如下:
1.添加配置类(和@SpringBootApplication注解类在同一级目录)
package com.admin.monitor;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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;/*** Swagger2 配置文件* @author 三多* @Time 2019/3/18*/
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Beanpublic Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.admin.monitor.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("大数据-监控系统").description("关注:xxxx").termsOfServiceUrl("http://localhost:8201").contact("sanduo").version("v1.0").build();}}
其中有一下几点需要注意:
- 添加注解开启Swagger扫描 ,@EnableSwagger2;
- 将配置交由Spring ,@Configuration;
2.在添加配置类-解决404 资源加载不到问题
package com.admin.monitor.config;import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.util.List;/*** 解决前台返回Long类型精度损失的问题* @author 三多* @Time 2019/3/25*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
/*** 解决前端解析Long丢精度的问题* @param converters*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();ObjectMapper objectMapper = new ObjectMapper();/*** 序列换成json时,将所有的long变成string* 因为js中得数字类型不能包含所有的java long值*/SimpleModule simpleModule = new SimpleModule();simpleModule.addSerializer(Long.class, ToStringSerializer.instance);simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);objectMapper.registerModule(simpleModule);jackson2HttpMessageConverter.setObjectMapper(objectMapper);converters.add(jackson2HttpMessageConverter);}/*** 解决 swagger-ui404问题* @param registry*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");super.addResourceHandlers(registry);}}
其中有一下几点需要注意:
- 将配置交由Spring ,@Configuration;
- **注意不是集成WebMvcConfigurationSupport,而是实现WebMvcConfigurer **
3.启动类不要添加@EnableWebMvc
//@EnableWebMvc
@SpringBootApplication
public class AdminServerApp {
public static void main(String[] args) {
SpringApplication.run(AdminServerApp.class,args);}
}
原因:
- @EnableWebMvc:底层使用了WebMvcConfigurationSupport,而替换WebMvcConfigurationSupport致使自动配置失效
4.完美启动
访问:http://localhost:8201/swagger-ui.html
5.参考
1.SpringBoot2.x中WebMvcConfigurerAdapter已过时,使用WebMvcConfigurationSupport替换时自动配置失效