您的位置:首页 > 编程语言 > Java开发

springboot 系统学习2 -- fastjson

2017-10-14 18:22 381 查看
首先添加fastjson的依赖

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"}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: