spring boot 解决后台返回 json 到前台中文乱码,报错 500:no convertter for return value of type,中文返回给浏览器乱码 解析成问号?? 详细错误信息Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:No converter found for return value of type: class www.it399.com.controller.file.ResultBean
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter {
@Bean
public HttpMessageConverter responseBodyConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
@Override
public void configureMessageConverters(List> converters) {
super.configureMessageConverters(converters);
//解决中文乱码
converters.add(responseBodyConverter());
//解决 添加解决中文乱码后 上述配置之后,返回json数据直接报错 500:no convertter for return value of type
converters.add(messageConverter());
}
}
@Bean
public MappingJackson2HttpMessageConverter messageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(getObjectMapper());
return converter;
}
/**
* spring boot 解决后台返回 json 到前台出现中文乱码的问题
* http://www.it399.com/blog/index.jsp
*/
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter {
@Bean
public HttpMessageConverter responseBodyConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
@Bean
public ObjectMapper getObjectMapper() {
return new ObjectMapper();
}
@Bean
public MappingJackson2HttpMessageConverter messageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(getObjectMapper());
return converter;
}
@Override
public void configureMessageConverters(List> converters) {
super.configureMessageConverters(converters);
//解决中文乱码
converters.add(responseBodyConverter());
//解决 添加解决中文乱码后 上述配置之后,返回json数据直接报错 500:no convertter for return value of type
converters.add(messageConverter());
}
}
转载请注明原文链接:http://www.it399.com/blog/web/201805081017
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28470105/viewspace-2156254/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28470105/viewspace-2156254/