Remember the problems in SpringBoot1. * to Springoot2.0

1. Interceptor problem

Write static path pattern in the configuration file after 2.0. / static / * * has no effect (2.0 needs to be configured in the method)

 

SpringBoot1. *


@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    private final UserMethodArgumentResolver userMethodArgumentResolver;
    @Autowired
    public JwtInterceptor(UserMethodArgumentResolver userMethodArgumentResolver) {
        this.userMethodArgumentResolver = userMethodArgumentResolver;

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getWebmvcConfig())
                .addPathPatterns("/**")//intercept
                .excludePathPatterns("/",//Exclude interception
          "/login/**"); super.addInterceptors(registry); } @Bean public WebmvcConfig getWebmvcConfig() { return new WebmvcConfig(); } @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(userMethodArgumentResolver); } }

Spring boot 2.0

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    private static final Logger LOGGER = LoggerFactory.getLogger(WebMvcConfig.class);


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {//Set resource path
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JwtInterceptor())
                .addPathPatterns("/**")//intercept
                .excludePathPatterns("/",//Exclude interception"/login/**","/static/**");
    }

}

 

2. Coding problems

Page access will not appear garbled, but the Chinese returned by Ajax request will appear garbled. Through investigation, it is because the ResponseBody refers to the class StringHttpMessageConverter, and the default code of this class is iso_. We need to change it to UTF-8.

This code segment is placed under the WebMvcConfig class that inherits the WebMvcConfigurationSupportl class

 

    /**
     * Set the default encoding to UTF-8
     */
    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        final StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converter.setWriteAcceptCharset(false);
        return converter;
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(responseBodyConverter());
        addDefaultHttpMessageConverters(converters);
    }
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

The default StringHttpMessageConverter will add the accept charset response header with all character sets. It needs to be set as follows

  converter.setWriteAcceptCharset(false);






Tags: Java Spring encoding

Posted on Wed, 04 Dec 2019 12:35:48 -0500 by banzaimonkey