springBoot,默认使用的json解析框架是Jackson。
虽然jackson能够满足json的解析,如果想使用熟悉的alibaba的fastjon,我们只需要在pom文件中配置maven依赖就好。
版本可根据自己需要查询,网址:http://mvnrepository.com/Maven依赖完成之后,我们可以通过两种方式配置fastjon
方法一:启动类继承extends WebMvcConfigurerAdapter,且覆盖configureMessageConverters。
方法二:在启动类中,注入Bean:HttpMessageConverters。
代码如下:
package org.lzm.springbootnew;
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.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.ArrayList;
import java.util.List;
@EnableScheduling //定时任务
@SpringBootApplicationpublic class SpringbootNewApplication extends WebMvcConfigurerAdapter {
-
​public static void main(String[] args) {​
-
​ SpringApplication.run(SpringbootNewApplication.class, args);​
-
​}​
-
​ /*​
-
​ * // 方法一:extends WebMvcConfigurerAdapter​
-
​ */​
-
​@Override​
-
​public void configureMessageConverters(List<httpmessageconverter<?>> converters) {​</httpmessageconverter<?>
-
​ super.configureMessageConverters(converters);​
-
​ //1、先定义一个convert转换消息的对象​
-
​ FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();​
-
​ //2、添加fastjson的配置信息,比如是否要格式化返回的json数据;​
-
​ FastJsonConfig fastJsonConfig=new FastJsonConfig();​
-
​ fastJsonConfig.setSerializerFeatures(​
-
​ //是否需要格式化​
-
​ SerializerFeature.PrettyFormat​
-
​ );​
-
​ //附加:处理中文乱码(后期添加)​
-
​ List<mediatype> mediaTypeList=new ArrayList<mediatype>();​</mediatype></mediatype>
-
​ mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);​
-
​ fastConverter.setSupportedMediaTypes(mediaTypeList);​
-
​ //3、在convert中添加配置信息​
-
​ fastConverter.setFastJsonConfig(fastJsonConfig);​
-
​ //4、将convert添加到converters​
-
​ converters.add(fastConverter);​
-
​}​
-
​/*​
-
​ * 方法二:在启动类中,注入Bean:HttpMessageConverters​
-
​ */​
-
​@Bean​
-
​public HttpMessageConverters fastJsonHttpMessageConverters(){​
-
​ //1、先定义一个convert转换消息的对象​
-
​ FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();​
-
​ //2、添加fastjson的配置信息,比如是否要格式化返回的json数据;​
-
​ FastJsonConfig fastJsonConfig=new FastJsonConfig();​
-
​ fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);​
-
​ //附加:处理中文乱码​
-
​ List<mediatype> fastMedisTypes = new ArrayList<mediatype>();​</mediatype></mediatype>
-
​ fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);​
-
​ fastConverter.setSupportedMediaTypes(fastMedisTypes);​
-
​ //3、在convert中添加配置信息​
-
​ fastConverter.setFastJsonConfig(fastJsonConfig);​
-
​ HttpMessageConverter<?> converter=fastConverter;​
-
​ return new HttpMessageConverters(converter);​
-
​}​
}
其实代码的核心是相同的,这是调取的方式不同而已。两种方式都可以满足我们对于fastjson的依赖使用。
下面使用@JSONField()注解在实体类中进行验证
代码如下。
@JSONField(format = “yyyy-MM-dd”)
private Date birthday;
Controller中代码如下。
@RequestMapping(“/save”)
public Student save(){
Student student=new Student();
student.setName(“婷婷”);
student.setAge(23);
student.setSex(“女”);
student.setBirthday(new Date());
studentService.save(student);
return student;
}
浏览器返回结果:
{
“age”:23,
“birthday”:”2018-07-10″,
“id”:97,
“name”:”婷婷”,
“sex”:”女”
}
除此之外,我们还可以通过@JSONField(serialize = false)来决定字段的显示与否。设置如下。
@JSONField(serialize = false)
private Date birthday;
如果这样设置浏览器返回结果如下,birthday将不再显示。
{
“age”:23,
“id”:97,
“name”:”婷婷”,
“sex”:”女”
}
Original: https://blog.51cto.com/u_15659525/5339757
Author: 明天继续飞
Title: Spring Boot fastJSON的使用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/517704/
转载文章受原作者版权保护。转载请注明原作者出处!