sprngboot中使用fastjson 出现乱码的解决方法

在使用下面的代码进行返回Demo实体的时候出现中文乱码问题,代码如下:

APP启动类:

@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> converters) {
		super.extendMessageConverters(converters);
		// 定义convert
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig config = new FastJsonConfig();
		config.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastJsonHttpMessageConverter.setFastJsonConfig(config);
		converters.add(fastJsonHttpMessageConverter);
	}
}

Demo类:

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类:

@RestController
public class HelloContorller {

	@RequestMapping("/getDemo1")
	public Demo getDemo1()
	{
		Demo demo = new Demo();
		demo.setId(1);
		demo.setName("优质青年");
		demo.setCreateTime(new Date());
		return demo;
	}
}

在通过浏览器访问getDemo1的出现下面的现象:

{"createTime":"2018-04-22 13:23","id":1,"name":"浼樿川闈掑勾"}

即:优质青年 变成了 浼樿川闈掑勾

解决问题最重要,下面直接附上代码:

@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> converters) {
		super.extendMessageConverters(converters);
		// 定义convert
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig config = new FastJsonConfig();
		config.setSerializerFeatures(SerializerFeature.PrettyFormat);
		// 处理中文的乱码问题
		// 创建MediaType的集合
		List supportedMediaTypes = new ArrayList();
		// 设置编码格式为UTF8
		supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		// 将supportedMediaTypes对象赋值给fastJsonHttpMessageConverter的SupportedMediaTypes属性
		fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes );
		fastJsonHttpMessageConverter.setFastJsonConfig(config);
		converters.add(fastJsonHttpMessageConverter);
	}
}

再次访问原路径,效果如下:

{
    "createTime":"2018-04-22 13:28",
    "id":1,
    "name":"优质青年"

}

这样就解决了问题。当然了如果是通过@Bean的方式实现的fastjson转换的方式,处理方式也是一样的,如:

@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> converters) {
		super.extendMessageConverters(converters);
		// 定义convert
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig config = new FastJsonConfig();
		config.setSerializerFeatures(SerializerFeature.PrettyFormat);
		// 处理中文的乱码问题
		// 创建MediaType的集合
		List supportedMediaTypes = new ArrayList();
		// 设置编码格式为UTF8
		supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		// 将supportedMediaTypes对象赋值给fastJsonHttpMessageConverter的SupportedMediaTypes属性
		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);
		// 处理中文的乱码问题
		// 创建MediaType的集合
		List supportedMediaTypes = new ArrayList();
		// 设置编码格式为UTF8
		supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		// 将supportedMediaTypes对象赋值给fastJsonHttpMessageConverter的SupportedMediaTypes属性
		fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes );
		fastJsonHttpMessageConverter.setFastJsonConfig(config);
		return new HttpMessageConverters(fastJsonHttpMessageConverter);
	}
}


你可能感兴趣的:(fastjson,中文乱码)