springboot 系统学习2 -- fastjson
2017-10-14 18:22
381 查看
首先添加fastjson的依赖
springboot 集成fastjson 有两种方法
1.第一种继承WebMvcConfigurerAdapter,重写configureMessageConverters方法:
2.第二种方式bean注入HttpMessageConverters
测试方法
在返回测试类中
postman 请求
如果将测试类的@JSONField(serialize = false)去掉的话
说明 已经成功了
当然,项目中用到 json的地方很过, 所以肯定会需要jsonUtil 来作为工具类下面简单的写一个json工具类
测试方法
运行得到如下输出
compile 'com.alibaba:fastjson:1.2.38'
springboot 集成fastjson 有两种方法
1.第一种继承WebMvcConfigurerAdapter,重写configureMessageConverters方法:
2.第二种方式bean注入HttpMessageConverters
package smaug.cloud.provider; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.glassfish.jersey.servlet.ServletContainer; import org.glassfish.jersey.servlet.ServletProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import smaug.cloud.config.jerseryConfig.AnnotationJerseyConfig; import java.util.ArrayList; import java.util.List; /** * Created by Allen on 17/10/10. */ @SpringBootApplication @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) public class SmaugCloudApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(SmaugCloudApplication.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<?>> converterList) { super.configureMessageConverters(converterList); //定义一个converter FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); List<MediaType> types = new ArrayList<>(); types.add(MediaType.APPLICATION_JSON_UTF8); //默认utf8 fastJsonHttpMessageConverter.setSupportedMediaTypes(types); fastJsonHttpMessageConverter.setFastJsonConfig(config); converterList.add(fastJsonHttpMessageConverter); } // @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); // } }
测试方法
在返回测试类中
package smaug.cloud.api.vos.test; import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; /** * Created by Allen on 17/10/10. */ @Data public class TestResponse { private int id; private String name; @JSONField(serialize = false) private String address; }
postman 请求
{ "id": 1, "name": "13" }
如果将测试类的@JSONField(serialize = false)去掉的话
{ "address": "上海", "id": 1, "name": "13" }
说明 已经成功了
当然,项目中用到 json的地方很过, 所以肯定会需要jsonUtil 来作为工具类下面简单的写一个json工具类
public interface JsonUtil { String toJson(Object o); }
public class FastJsonUtil implements JsonUtil { @Override public String toJson(Object o) { if (null == o) { return null; } return JSONObject.toJSONString(o); } }
测试方法
public TestResponse getUser() { TestResponse response = new TestResponse(); response.setId(1); response.setName("13"); response.setAddress("上海"); logger.info(jsonUtil.toJson(response)); return response; }
运行得到如下输出
2017-10-14 18:19:13.374 WARN 88262 --- [nio-8080-exec-1] o.g.jersey.server.ApplicationHandler : Component of class interface smaug.cloud.api.interfaces.TestService cannot be instantiated and will be ignored. 2017-10-14 18:19:13.834 INFO 88262 --- [nio-8080-exec-1] s.cloud.provider.impl.TestServiceImpl : {"address":"上海","id":1,"name":"13"}
相关文章推荐
- Spring Boot 学习4--完美使用FastJson解析Json数据
- 【系统学习SpringBoot】SpringBoot 对象转JSON输出
- SpringBoot学习:Spring Boot使用FastJson解析JSON数据
- spring-boot学习之fastjson
- SpringBoot学习-(2)使用FastJson作为json解析器
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- SpringBoot之fastjson(M)
- 【系统学习SpringBoot】SpringBoot初遇邮件服务
- Springboot+fastJson自定义消息收发器详细实例
- Spring-boot中使用fastjson作为json框架
- 【系统学习SpringBoot】再遇Spring Data JPA之JPA应用详解(自定义查询及复杂查询)
- Spring Boot完美使用FastJson解析JSON数据(十三)
- 【系统学习SpringBoot】SpringBoot读取Property配置
- springboot 系统学习5 集成 activemq
- 【系统学习SpringBoot】springBoot +IDEA 热部署
- SpringBoot(十):SpringBoot使用FastJson
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Spring Boot 学习2--Spring Boot返回json数据
- SpringBoot(十):SpringBoot使用FastJson