Spring Boot系列(三):Spring Boot转化为json数据格式

Spring Boot为我们良好的提供了我们需要的数据,将数据转化为json格式,然后返回,

下面请看springboot转化为json的方式;

第一种方式:

SpringBoot框架默认的方式;

步骤:

* 1.编写实体类student;

package com.gmm;

/**测试的实体类  * Created by john on 2017-04-30.  */ public class Student {
    private Integer sId;
    private String sName;
    private String gender;

    public Integer getsId() {
        return sId;
    }

    public void setsId(Integer sId) {
        this.sId = sId;
    }

    public String getsName() {
        return sName;
    }

    public void setsName(String sName) {
        this.sName = sName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

* 2.编写getStudernt()方法在controller中;

写法:创建一个学生对象设置学生属性信息数据然后获取以json的方式获取这些数据;

package com.gmm;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**  * Created by john on 2017-04-30.  */  @RestController
public class Controller {


    /**  * springboot默认使用的json解析框架是jackson;  * @return  */  @RequestMapping("/getStudent")
    public Student getStudent(){
        Student student = new Student();
        student.setGender("");
        student.setsName("莉香");
        student.setsId(1);
        return student;
    }
}
注意:
@RestController 此注解是代表当前类是一个springboot应用程序;

@RequestMapping("/getStudent") 此注解是请求的映射路径;

* 3.进行测试;

编写一个Springboot的入口程序;

package com.gmm;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**  * Created by john on 2017-04-30.  */  @SpringBootApplication
public class Application {

    private static Logger logger = Logger.getLogger(Application.class);


    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
        logger.info("=====spring boot start success====");
    }
}
注:访问路径为 http://localhost:8080/getStudent

谷歌游览器页面显示的结果为:

{"sId":1,"sName":"莉香","gender":"女"}

以上我们了解到SpringBootmoren 默认使用的json解析技术框架是jackson;

在maven下载的架包中可以看到有一个jackson.jar架包;

以上是第一种解析方式,通常我们也是用其它的解析方式;

第二种方式;

Spring Boot使用FastJson解析JSON数据

步骤

* 1.在上面pom,xml文件依赖的基础上添加FastJson的依赖包;

完整pom文件

xml version="1.0" encoding="UTF-8"?>
xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    springbootjson
    com.gmm
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.1.RELEASE
    

    
        UTF-8
        
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
            test
        
        
            com.alibaba
            fastjson
            1.2.15
        

    
在这个地方我们要强调一下,在官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+。具体看文档!

* 2.配置fastjon(支持两种方法);

第一种方法:

一:启动类继承extends WebMvcConfigurerAdapter

二:覆盖方法configureMessageConverters

package com.gmm;

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

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

/** springbootfastjon方式转化json数据;  * Created by john on 2017-04-30.  */ @SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {
    private static Logger logger = Logger.getLogger(Application.class);

    @Override
    public void configureMessageConverters(List> converters) {
        super.configureMessageConverters(converters);
        //1.需要定义一个convert转换消息的对象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //3处理中文乱码问题
        List fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        //4.convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        //5.convert添加到converters当中.
        converters.add(fastJsonHttpMessageConverter);
    }
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
        logger.info("=====spring boot start success====");
    }
}

三:在启动测试实体类中添加一个属性;并提供getset方法;

@JSONField(format = "yyyy-MM-dd HH:mm")
private Date createTime;

public Date getCreateTime() {
    return createTime;
}

public void setCreateTime(Date createTime) {
    this.createTime = createTime;
}
@JSONField(format = "yyyy-MM-dd HH:mm")此注解是fastjon包提供的用于指定将数据返回什么样的json格式数据;
四:在controller中在将添加的实体属性给加进去;

@RequestMapping("/getStudent")
public Student getStudent(){
    Student student = new Student();
    student.setGender("");
    student.setsName("莉香");
    student.setsId(1);
    student.setCreateTime(new Date());
    return student;
}
五:开启springboot测试结果;

结果为:

Spring Boot系列(三):Spring Boot转化为json数据格式_第1张图片

说明springboot按照fastjson的方式将数据以json格式解析出来了;

第二种方法:

一:启动测试类中注入Bean: HttpMessageConverters

具体做法:删除继承extends WebMvcConfigurerAdapter;

      删除重写的方法改为以下代码;

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
    //1.需要定义一个convert转换消息的对象;
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    //2:添加fastJson的配置信息;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    //3处理中文乱码问题
    List fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    //4.convert中添加配置信息.
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter converter = fastJsonHttpMessageConverter;
    return new HttpMessageConverters(converter);

}
二:在入口springboot类中测试结果;

Spring Boot系列(三):Spring Boot转化为json数据格式_第2张图片

结果显示和上面的结果是一样的所有两种方式都是有效的,具体使用哪种方法我感觉还是看你自己了!以上暂时结束!


 
  

你可能感兴趣的:(spring-boot)