springboot整合fastjson,解决了中文乱码问题

1.排除 spring-boot-starter-json依赖,添加fastjson依赖。



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.4.RELEASE
		 
	
	com.zhong
	chatper07
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-web
			
				
					org.springframework.boot
					spring-boot-starter-json
				
			
		
		
			org.springframework.boot
			spring-boot-devtools
			runtime
		
		
			org.projectlombok
			lombok
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			com.alibaba
			fastjson
			1.2.46
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

2.添加HttpMessageConverters配置

package com.zhong.chatper07.config;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

/**
 * HttpConverterConfig
 */
@Configuration
public class HttpConverterConfig {

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 1.定义一个converters转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 空值特别处理
        // WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
        // WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
        // WriteNullNumberAsZero 将数值类型字段的空值输出为0
        // WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty);

        // 处理中文乱码问题
        List fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        
        // fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 3.在converter中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        // 4.将converter赋值给HttpMessageConverter
        HttpMessageConverter converter = fastConverter;
        // 5.返回HttpMessageConverters对象
        return new HttpMessageConverters(converter);
    }
}

3.实体类使用@JSONField注解配置字段的是否需要序列化,格式等。

package com.zhong.chatper07.entity;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.support.spring.annotation.FastJsonView;

import org.springframework.format.annotation.DateTimeFormat;

import lombok.Data;

/**
 * User
 */
@Data
public class User {

    private Integer id;
    private String name;

    @JSONField(serialize=false)  
    private String password;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JSONField(format="yyyy-MM-dd")
    private Date createDate;
    
}

4.编写控制类,返回json数据

package com.zhong.chatper07.controller;

import java.util.Date;

import com.zhong.chatper07.entity.User;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * UserController
 */
@RestController
public class UserController {

    @GetMapping("/user/{id}")
    public User findUserById(@PathVariable Integer id){
        User user = new User();
        user.setId(1);
        user.setName("中文");
        user.setCreateDate(new Date());
        user.setPassword("111");
        return  user;
    }
    
}

springboot整合fastjson,解决了中文乱码问题_第1张图片

你可能感兴趣的:(SpringBoot)