CustomMVCConfiguration.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package com.java110.service.configuration;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.http.converter.HttpMessageConverter;
  5. import org.springframework.http.converter.StringHttpMessageConverter;
  6. import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
  7. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  8. import java.nio.charset.Charset;
  9. import java.util.List;
  10. /**
  11. * 前台提交乱码解决
  12. * Created by wuxw on 2018/5/14.
  13. */
  14. @Configuration
  15. public class CustomMVCConfiguration extends WebMvcConfigurerAdapter {
  16. @Bean
  17. public HttpMessageConverter<String> responseBodyConverter() {
  18. StringHttpMessageConverter converter = new StringHttpMessageConverter(
  19. Charset.forName("UTF-8"));
  20. return converter;
  21. }
  22. @Override
  23. public void configureMessageConverters(
  24. List<HttpMessageConverter<?>> converters) {
  25. super.configureMessageConverters(converters);
  26. converters.add(responseBodyConverter());
  27. }
  28. @Override
  29. public void configureContentNegotiation(
  30. ContentNegotiationConfigurer configurer) {
  31. configurer.favorPathExtension(false);
  32. }
  33. /**
  34. * +对于header中的中文字进行解码
  35. *
  36. * @return 转换结果
  37. */
  38. @Bean
  39. public StringDecoderForHeaderConverter stringHeaderConverter() {
  40. return new StringDecoderForHeaderConverter(Charset.forName("UTF-8"));
  41. }
  42. }