【Spring boot 使用其他 json 转换框架】

Spring boot 使用其他 json 转换框架
个人使用比较习惯的 json 框架是 fastjson,所以 spring boot 默认的 json 使用起来就很陌生了,所以很自然我就
想我能不能使用 fastjson 进行 json 解析呢?
< dependencies >
< dependency >
< groupId > com.alibaba groupId >
< artifactId > fastjson artifactId >
< version > 1.2.15 version >
dependencies >
这里要说下很重要的话,官方文档说的 1.2.10 以后,会有两个方法支持 HttpMessageconvert,一个是
FastJsonHttpMessageConverter,支持 4.2 以下的版本,一个是 FastJsonHttpMessageConverter4 支持 4.2 以
上的版本,具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是
1.2.10+。
配置 fastjon
支持两种方法:
第一种方法:
(1)启动类继承 extends WebMvcConfigurerAdapter
(2)覆盖方法 configureMessageConverters
第二种方法:
(1)在 App.java 启动类中,注入 Bean : HttpMessageConverters
具体代码如下:
代码:App.java
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.spring.FastJsonHttpMessageConverter;
//如果想集成其他的json框架需要继承WebMvcConfigurerAdapter,并重写configureMessageConverters
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter {
// 第一种方式,重写configureMessageConverters,并将FastJsonConverter设置到系统中
@Override
public void configureMessageConverters(List> converters ) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter .setFeatures(SerializerFeature. PrettyFormat );
converters .add( converter );
super .configureMessageConverters( converters );
}
// 第二种方法:注入beanHttpMessageConverters
/*
* @Bean public HttpMessageConverters faMessageConverters(){
* return new HttpMessageConverters(new FastJsonHttpMessageConverter()); }
*/
public static void main(String[] args ) {
SpringApplication. run (App. class , args );
}
}

你可能感兴趣的:(springboot,java,spring,spring,boot)