java自定义json解析器,SpringBoot使用自定义json解析器的使用方法

Spring-Boot是基于Spring框架的,它并不是对Spring框架的功能增强,而是对Spring的一种快速构建的方式。

Spring-boot应用程序提供了默认的json转换器,为Jackson。示例:

pom.xml中dependency配置:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.qinker

spring-boot

war

org.springframework.boot

spring-boot-starter-parent

2.0.0.RELEASE

0.0.1-SNAPSHOT

spring-boot

http://maven.apache.org

UTF-8

9

org.springframework.boot

spring-boot-starter-web

spring-boot

复制代码

创建三个类:MainApp.java和User.java以及HelloController.java:

package com.springboot;

import java.util.Date;

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

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

@RestController

public class HelloController {

@RequestMapping("/hello")

public String hello() {

return "hello,SpringBoot";

}

/**

* Spring boot 默认json解析框架是Jackson

* @return

*/

@RequestMapping("/getUser")

public User getUser() {

User u = new User();

u.setName("张三");

u.setAge(33);

u.setCreateTime(new Date());

return u;

}

}

复制代码

package com.springboot;

import java.io.Serializable;

import java.util.Date;

public class User implements Serializable{

private String name;

private int age;

private Date createTime;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

}

复制代码

package com.springboot;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class MainApp{

public static void main(String[] args) {

SpringApplication.run(MainApp.class, args);

}

}

复制代码

启动MainApp:访问http://localhost:8080/getUser,结果如下:

{"name":"张三","age":33,"createTime":"2018-04-04T03:03:08.534+0000"}

复制代码

可见:我们并未做任何配置,返回的却是json数据,可见Spring-Boot对json做了默认实现,使用的是内置Jackson转换器。

那么,下面看看如何使用自定义的json转换器,这里以fastjson为例:

首先,引入fastjson包,在pom中添加如下依赖:

com.alibaba

fastjson

1.2.47

复制代码

为了方便看出效果:修改User类:

package com.springboot;

import java.io.Serializable;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

@SuppressWarnings("serial")

public class User implements Serializable{

private String name;

private int age;

@JSONField(format="yyyy-MM-dd HH:mm")

private Date createTime;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

}

复制代码

1.实现fastjson自定义json转换的第一种方式,Spring-Boot实现WebMvcConventer接口:

修改MainApp如下:

package com.springboot;

import java.util.ArrayList;

import java.util.List;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.http.MediaType;

import org.springframework.http.converter.HttpMessageConverter;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.serializer.SerializerFeature;

import com.alibaba.fastjson.support.config.FastJsonConfig;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication

public class MainApp implements WebMvcConfigurer{

@Override

public void configureMessageConverters(List> converters) {

WebMvcConfigurer.super.configureMessageConverters(converters);

//创建fastjson转换器实例

FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

//配置对象

FastJsonConfig config = new FastJsonConfig();

List mediaTypes = new ArrayList<>();

//中文编码

MediaType mediaType = MediaType.APPLICATION_JSON_UTF8;

mediaTypes.add(mediaType);

config.setSerializerFeatures(SerializerFeature.PrettyFormat);

converter.setSupportedMediaTypes(mediaTypes);

converter.setFastJsonConfig(config);

converters.add(converter);

}

public static void main(String[] args) {

SpringApplication.run(MainApp.class, args);

}

}

复制代码

启动程序:访问上面的路径:浏览器会看到如下结果:

{

"age":33,

"createTime":"2018-04-04 11:14",

"name":"张三"

}

复制代码

2.使用@Bean注解注入fastjson转换器:修改MainApp如下:

package com.springboot;

import java.util.ArrayList;

import java.util.List;

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 com.alibaba.fastjson.serializer.SerializerFeature;

import com.alibaba.fastjson.support.config.FastJsonConfig;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication

public class MainApp{

[url=home.php?mod=space&uid=4377]@Bean[/url]

public HttpMessageConverters fastJsonHttpMessageConventers() {

FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

FastJsonConfig config = new FastJsonConfig();

config.setSerializerFeatures(SerializerFeature.PrettyFormat);

List mediaTypes = new ArrayList<>();

mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);

converter.setSupportedMediaTypes(mediaTypes);

return new HttpMessageConverters(converter);

}

public static void main(String[] args) {

SpringApplication.run(MainApp.class, args);

}

}

复制代码

访问结果是一样的。

以上就是本文的全部内容,希望对大家的学习有所帮助,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值