您的位置:首页 > 编程语言 > Java开发

Spring Boot入门之使用第三方JSON框架

2018-03-06 22:15 531 查看

笔记摘要

本文以Spring Boot使用fastjson替代原有JSON框架为例,介绍Spring Boot如何使用第三方JSON框架。

Spring Boot使用自定义JSON框架方法

方法一

在pom.xml中引入相关依赖;

在App类中继承WebMvcConfigurerAdapter并重载configureMessageConverters方法,增加自定义的JSON解析框架。

方法二

在pom.xml中引入相关依赖;

使用@Bean引入第三方框架。

实验过程

实验过程一:

Created with Raphaël 2.1.2开始配置pom.xml文件,引入依赖包在App类中继承WebMvcConfigurerAdapter方法重载configureMessageConverters方法编写GetJsonDemo类用于生成JSON字符串修改MyController类,增加getDemo方法结束

实验过程二:

Created with Raphaël 2.1.2开始配置pom.xml文件,引入依赖包在App类中注入Bean:HttpMessageConverters编写GetJsonDemo类用于生成JSON字符串修改MyController类,增加getDemo方法结束

实验代码

pom.xml

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>


方法一App类代码

package my.spring.helloworld;

import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication
public class App extends WebMvcConfigurerAdapter
{

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

/*
* 1.定义convert,转换消息的对象;
* 2.添加fastjson配置信息;
* 3.在convert中添加配置信息;
* 4.将convert添加到converters。
*/
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

converters.add(fastJsonHttpMessageConverter);
}

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


方法二App类代码

package my.spring.helloworld;

import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure
b030
.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication
public class App
{
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

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

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


GetJsonDemo类代码

package my.spring.helloworld;

import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;

public class GetJsonDemo {
private int id;
private String name;
@JSONField(format="yy-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;
}
}


MyController类代码

@RequestMapping("/getJsonDemoData")
@ResponseBody
public GetJsonDemo getDemo() {
GetJsonDemo demo = new GetJsonDemo();
demo.setId(1);
demo.setName("JsonDemoData");
demo.setCreateTime(new Date());
return demo;
}


实验结果

访问http://localhost:8080/getJsonDemoData,浏览器反馈

{ “createTime”:”18-03-06 20:48”, “id”:1, “name”:”JsonDemoData” }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring boot