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 此注解是代表当前类是一个Restful API的controller;


@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"?>
<project 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springbootjson</groupId>
    <artifactId>com.gmm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>

    </dependencies>
</project>
在这个地方我们要强调一下,在官方文档说的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<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        //1.需要定义一个convert转换消息的对象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = 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);
        //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测试结果;

结果为:


说明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<MediaType> 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类中测试结果;


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

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Spring Boot中的JPA(Java Persistence API)实现将JSON数据写入MySQL数据库。JPA提供了一种简单的方式来管理对象关系映射(ORM),即将Java对象映射到关系型数据库中的表。 以下是一个简单的示例: 1. 创建一个实体类,用于映射JSON数据到数据库表中的字段。 ```java @Entity @Table(name = "person") public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; // getters and setters } ``` 2. 创建一个Spring Data JPA repository接口,用于定义CRUD操作。 ```java @Repository public interface PersonRepository extends JpaRepository<Person, Long> { } ``` 3. 在Controller中获取JSON数据,并将其换为Person对象,然后使用PersonRepository将其保存到数据库中。 ```java @RestController @RequestMapping("/person") public class PersonController { @Autowired private PersonRepository personRepository; @PostMapping("/save") public void savePerson(@RequestBody Person person) { personRepository.save(person); } } ``` 这里假设JSON数据的格式与Person类的属性相匹配。如果不匹配,可以使用Jackson库进行换。 另外,需要在application.properties文件中配置MySQL数据库连接信息,例如: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=myusername spring.datasource.password=mypassword spring.datasource.driver-class-name=com.mysql.jdbc.Driver ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值