今天配置了一下消息转换器,测试的时候发现中文是乱码的,tomcat和mysql的编码设置没有问题,logger出来也是正常的,因此发现应该是消息转换器没有设置好MediaType,修正了一下:
@Override public void configureMessageConverters( List<HttpMessageConverter<?>> converters ) { converters.add(new ByteArrayHttpMessageConverter()); StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); stringHttpMessageConverter.setSupportedMediaTypes(Arrays.asList( new MediaType("text", "plain", Charset.forName("utf-8")), new MediaType("text", "html", Charset.forName("utf-8")) )); converters.add(stringHttpMessageConverter); converters.add(new FormHttpMessageConverter()); converters.add(new SourceHttpMessageConverter<>()); //add json converter MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); jsonConverter.setSupportedMediaTypes(Arrays.asList( new MediaType("application", "json", Charset.forName("utf-8")), new MediaType("text", "json", Charset.forName("utf-8")), new MediaType("application", "x-www-form-urlencoded", Charset.forName("utf-8")) )); jsonConverter.setObjectMapper(this.objectMapper); converters.add(jsonConverter); //add xml converter MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(); xmlConverter.setSupportedMediaTypes(Arrays.asList( new MediaType("application", "xml", Charset.forName("utf-8")), new MediaType("text", "xml", Charset.forName("utf-8")) )); xmlConverter.setMarshaller(this.marshaller); xmlConverter.setUnmarshaller(this.unmarshaller); converters.add(xmlConverter); }