When using the following code to return the Demo entity, there is a problem of Chinese garbled characters. The code is as follows:
APP startup class:
@SpringBootApplication @ComponentScan("soringboot.hello") public class APP extends WebMvcConfigurerAdapter{ public static void main(String[] args) { SpringApplication.run(APP.class, args); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.extendMessageConverters(converters); // define convert FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonHttpMessageConverter.setFastJsonConfig(config); converters.add(fastJsonHttpMessageConverter); } }
Demo type:
public class Demo { private int id; private String name; //com.alibaba.fastjson.annotation.JSONField; @JSONField(format="yyyy-MM-dd HH:mm") private Date createTime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
controller class:
@RestController public class HelloContorller { @RequestMapping("/getDemo1") public Demo getDemo1() { Demo demo = new Demo(); demo.setId(1); demo.setName("Quality Youth"); demo.setCreateTime(new Date()); return demo; } }
When accessing getDemo1 through a browser, the following phenomenon occurs:
{"createTime":"2018-04-22 13:23","id":1,"name":"浼樿川闈掑勾"}
That is: high-quality young people have become
Solving the problem is the most important, and the code is directly attached below:
@SpringBootApplication @ComponentScan("soringboot.hello") public class APP extends WebMvcConfigurerAdapter{ public static void main(String[] args) { SpringApplication.run(APP.class, args); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.extendMessageConverters(converters); // define convert FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); // handle Chinese garbled characters // Create a collection of MediaTypes List<MediaType> supportedMediaTypes = new ArrayList<MediaType>(); // Set the encoding format to UTF8 supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); // Assign the supportedMediaTypes object to the SupportedMediaTypes property of fastJsonHttpMessageConverter fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes ); fastJsonHttpMessageConverter.setFastJsonConfig(config); converters.add(fastJsonHttpMessageConverter); } }
Visit the original path again, the effect is as follows:
{
"createTime":"2018-04-22 13:28",
"id":1,
"name":"优质青年"
}
This solved the problem. Of course, if it is the fastjson conversion method implemented by @Bean, the processing method is the same, such as:
@SpringBootApplication @ComponentScan("soringboot.hello") public class APP extends WebMvcConfigurerAdapter{ public static void main(String[] args) { SpringApplication.run(APP.class, args); } /*@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.extendMessageConverters(converters); // define convert FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); // handle Chinese garbled characters // Create a collection of MediaTypes List<MediaType> supportedMediaTypes = new ArrayList<MediaType>(); // Set the encoding format to UTF8 supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); // Assign the supportedMediaTypes object to the SupportedMediaTypes property of fastJsonHttpMessageConverter fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes ); fastJsonHttpMessageConverter.setFastJsonConfig(config); converters.add(fastJsonHttpMessageConverter); }*/ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); // handle Chinese garbled characters // Create a collection of MediaTypes List<MediaType> supportedMediaTypes = new ArrayList<MediaType>(); // Set the encoding format to UTF8 supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); // Assign the supportedMediaTypes object to the SupportedMediaTypes property of fastJsonHttpMessageConverter fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes ); fastJsonHttpMessageConverter.setFastJsonConfig(config); return new HttpMessageConverters(fastJsonHttpMessageConverter); } }