springboot 整合fastjson
来源:互联网 发布:varier 知乎 编辑:程序博客网 时间:2024/05/22 05:26
个人觉得阿里爸爸的fastjson还是很好用的,所以这里就写一下sp集合fastjson的方法
首先在 gradle文件中加上fastjson 依赖
compile 'com.alibaba:fastjson:1.2.31'
其实springboot 整合fastjson 共有两种方法,现在就逐一说一下
- 启动类继承extends WebMvcConfigurerAdapter 并且覆盖方法configureMessageConverters
- 在App.java启动类中,注入Bean : HttpMessageConverters
现在说下第一种方法
在startApplication中
package smaug.service.provider.starts;import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;import org.glassfish.jersey.servlet.ServletContainer;import org.glassfish.jersey.servlet.ServletProperties;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ImportResource;import org.springframework.http.MediaType;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import smaug.service.config.jerseyConfig.AnnotationJerseyConfig;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;/** * Created by naonao on 17/7/9. */@SpringBootApplication(scanBasePackages = {"smaug.service", "smaug.util", "smaug.config"})@MapperScan("smaug.service.provider.mapper")@ImportResource("classpath:spring-order-context.xml")public class OrderStartApplication extends WebMvcConfigurerAdapter{ public static void main(String[] args) { SpringApplication.run(OrderStartApplication.class, args); } @Bean public ServletRegistrationBean jerseyServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*"); registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, AnnotationJerseyConfig.class.getName()); return registration; } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); //定义一个convert 转换消息对象 FastJsonHttpMessageConverter4 fastConverter = new FastJsonHttpMessageConverter4(); FastJsonConfig jsonConfig = new FastJsonConfig(); jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); List<MediaType> types = new ArrayList<>(); types.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(types); fastConverter.setFastJsonConfig(jsonConfig); converters.add(fastConverter); }}
第二种方法 在startAoolication 注入bean
package smaug.service.provider.starts;import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;import org.glassfish.jersey.servlet.ServletContainer;import org.glassfish.jersey.servlet.ServletProperties;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.web.HttpMessageConverters;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ImportResource;import org.springframework.http.converter.HttpMessageConverter;import smaug.service.config.jerseyConfig.AnnotationJerseyConfig;/** * Created by naonao on 17/7/9. */@SpringBootApplication(scanBasePackages = {"smaug.service", "smaug.util", "smaug.config"})@MapperScan("smaug.service.provider.mapper")@ImportResource("classpath:spring-order-context.xml")public class OrderStartApplication { public static void main(String[] args) { SpringApplication.run(OrderStartApplication.class, args); } @Bean public ServletRegistrationBean jerseyServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*"); registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, AnnotationJerseyConfig.class.getName()); return registration; }// @Override// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// super.configureMessageConverters(converters);// //定义一个convert 转换消息对象// FastJsonHttpMessageConverter4 fastConverter = new FastJsonHttpMessageConverter4();// FastJsonConfig jsonConfig = new FastJsonConfig();// jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);// List<MediaType> types = new ArrayList<>();// types.add(MediaType.APPLICATION_JSON_UTF8);// fastConverter.setSupportedMediaTypes(types);// fastConverter.setFastJsonConfig(jsonConfig);// converters.add(fastConverter);// } @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4(); FastJsonConfig jsonConfig = new FastJsonConfig(); jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setFastJsonConfig(jsonConfig); HttpMessageConverter<?> httpConverter = converter; return new HttpMessageConverters(httpConverter); }}
测试方法 将返回类 如此书写 将返回实体类加入一下注解,如果请求结果没有该属性,就表明成功了
@JSONField(serialize = false)private String shopName;
阅读全文
0 0
- springboot 整合fastjson
- springboot 整合resteasy+fastjson
- Spring Boot学习之旅:(四)springboot 整合 fastjson
- SpringBoot-01SpringBoot配置FastJson
- 2.SpringBoot-FastJson
- springboot使用fastjson
- SpringBoot中使用fastjson
- springboot使用fastjson
- springboot配置fastjson
- SpringBoot 使用fastjson初识
- Spring MVC整合fastjson
- SpringMVC 整合fastjson
- SpringMVC整合fastjson
- SpringMVC整合fastjson
- springMVC 与 fastjson整合
- Spring4.2+整合FastJson
- SpringMVC整合fastjson
- SpringMVC整合fastjson
- 关于MongoDB无法启动服务
- 电脑C盘那些常见文件夹的作用
- 设计模式之工厂方法模式
- spark streaming读取kafka数据令丢失(二)
- Windows下Anaconda2(Python2)和Anaconda3(Python3)的共存
- springboot 整合fastjson
- HDOJ 1735 字数统计
- VMware vSphere Web Services SDK编程指南(八)- 8.7 使用凭据库自动登录
- mybatits 一对多
- iOS导航栏遮挡问题的总结
- 基于TVUE框架在中型移动端项目的直出同构实践
- 最近在看《微服务设计》这本书。记录下自己的心得体会。
- maven基础之环境配置
- eclipse for javaEE