Springboot下swagger-ui.html访问不到

问题

在前面的Swagger2的基本配置中是可以访问到swagger-ui.html的
但当自定义继承配置WebMvcConfigurationSupport后便无法访问到该页面,原因参考请看参考资料。

首先看我的自定义配置,

*************
 * HttpMessageConverter转换处理
 * 处理转义hmtl标签为正常的hmtl标签
 * @author zjcjava@163.com
 *
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(
                DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.getSerializerProvider().setNullValueSerializer(
                new JsonSerializer() {
                    @Override
                    public void serialize(Object value, JsonGenerator jgen,
                            SerializerProvider provider) throws IOException,
                            JsonProcessingException {
                        jgen.writeString("");
                    }
                });
        // 进行HTML解码
        objectMapper.registerModule(new SimpleModule().addSerializer(
                String.class, new JsonSerializer() {
                    @Override
                    public void serialize(String value, JsonGenerator jgen,
                            SerializerProvider provider) throws IOException,
                            JsonProcessingException {
                        jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
                    }
                }));
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }

    @Override
    public void configureMessageConverters(
            List> converters) {
        converters.add(customJackson2HttpMessageConverter());
        super.addDefaultHttpMessageConverters(converters);
    }



} 
  

如果我把该文件全部注释掉则会正常显示,否则无法访问到swagger-ui.html,

解决方法

如果继承了WebMvcConfigurationSupport,则在配置文件在中配置的相关内容会失效,需要重新指定静态资源

因此参照资料2我做了处理如下,在该代码的末尾加上静态资源配置

代码加在

*************
 * HttpMessageConverter转换处理
 * 处理转义hmtl标签为正常的hmtl标签
 * @author zjcjava@163.com
 *
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(
                DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.getSerializerProvider().setNullValueSerializer(
                new JsonSerializer() {
                    @Override
                    public void serialize(Object value, JsonGenerator jgen,
                            SerializerProvider provider) throws IOException,
                            JsonProcessingException {
                        jgen.writeString("");
                    }
                });
        // 进行HTML解码
        objectMapper.registerModule(new SimpleModule().addSerializer(
                String.class, new JsonSerializer() {
                    @Override
                    public void serialize(String value, JsonGenerator jgen,
                            SerializerProvider provider) throws IOException,
                            JsonProcessingException {
                        jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
                    }
                }));
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }

    @Override
    public void configureMessageConverters(
            List> converters) {
        converters.add(customJackson2HttpMessageConverter());
        super.addDefaultHttpMessageConverters(converters);
    }

    /**
     * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
     * 
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }


    /**
     * 配置servlet处理
     */
    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

} 
  

参考资料

SpringBoot配置SwaggerUI访问404错误处理1
SpringBoot配置SwaggerUI访问404错误处理2

你可能感兴趣的:(springboot)