3、SpringBoot 返回JSON数据格式

SpringBoot 返回 JSON 数据格式

github源码

方式一:使用自带的 jackson

Controller 层的 @Controller 注解替换成 @RestController 即可

方式二:完美使用 FastJson

pom 引入依赖:

        <!-- 引入fastjson依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>

第一种方法(淘汰了)

  • 启动类继承 extends WebMvcConfigurerAdapter
  • 覆盖方法 configureMessageConverters
@SpringBootApplication
public class SpringbootJsonApplication extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        /*
		 * 1、需要先定义一个 convert 转换消息的对象;
		 * 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
		 * 3、在convert中添加配置信息.
		 * 4、将convert添加到converters当中.
		 *
		 */

		// 1、需要先定义一个 convert 转换消息的对象;
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

		//2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat
        );

		//3.处理中文乱码问题
		List<MediaType> fastMediaTypes =  new ArrayList<>();
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		fastConverter.setSupportedMediaTypes(fastMediaTypes);

		//4、在convert中添加配置信息.
        fastConverter.setFastJsonConfig(fastJsonConfig);

        //5、将convert添加到converters当中.
    	converters.add(fastConverter);

    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootJsonApplication.class, args);
    }

}

User

package cn.ylx.pojo;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.Date;

public class User {

    private int id;
    private String name;

    //com.alibaba.fastjson.annotation.JSONField
    @JSONField(format="yyyy-MM-dd HH:mm")
    private Date createTime;//创建时间.

    /*
     * 我们不想返回remarks?
     * serialize:是否需要序列化属性.
     */
    @JSONField(serialize=false)
    private String remarks;//备注信息.

    public String getRemarks() {
        return remarks;
    }
    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = 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;
    }

}

HelloController

@RestController
public class HelloController {

    /**
     * Spring Boot默认使用的json解析框架是jackson
     * @return
     */
    @RequestMapping("/getUser")
    public User getUser(){
        User user = new User();
        user.setId(1);
        user.setName("哈哈");
        user.setCreateTime(new Date());
        user.setRemarks("标记信息");
        return user;
    }

}

第二种方法(推荐)

  • 启动项注入Bean:HttpMessageConverters
    @Bean注入第三方的json解析框架:

启动项:

package cn.ylx;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

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

@SpringBootApplication
public class SpringbootJsonApplication {

	/**
	 * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
	 * @return
	 */
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		// 1、需要先定义一个 convert 转换消息的对象;
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

		//2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

		//3.处理中文乱码问题
		List<MediaType> fastMediaTypes =  new ArrayList<>();
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		fastConverter.setSupportedMediaTypes(fastMediaTypes);

		//3、在convert中添加配置信息.
		fastConverter.setFastJsonConfig(fastJsonConfig);

		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	}

    public static void main(String[] args) {
        SpringApplication.run(SpringbootJsonApplication.class, args);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_42112635/article/details/84675900