Spring Boot设置编码方式

1,修改application.yml

spring:
   application:
      name: administrative-division-crawler
   http:
      encoding:
         charset: UTF-8
         enabled: true
         force: true

2,增加Config配置类

@Configuration
public class SpringWebConfig extends WebMvcConfigurationSupport {

    /** 请求url忽略大小写 */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher pathMatcher = new AntPathMatcher();
        pathMatcher.setCaseSensitive(false);
        configurer.setPathMatcher(pathMatcher);
    }
    
    /** utf-8编码 */
    @Bean
    public HttpMessageConverter responseBodyConverter() {
        return new StringHttpMessageConverter(Charset.forName("UTF-8"));
    }
    
    /** 关键代码 */
    @Override
    public void configureMessageConverters(List> converters) {
        converters.add(responseBodyConverter());
        // 加载默认转换器
        addDefaultHttpMessageConverters(converters);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
}

3,设置maven编码


	
		
			org.apache.maven.plugins
			maven-compiler-plugin
			
				
				1.8
				1.8
				
				UTF-8
			
		
	

 

你可能感兴趣的:(JavaWeb)