springboot 整合fastjson

个人觉得阿里爸爸的fastjson还是很好用的,所以这里就写一下sp集合fastjson的方法

首先在 gradle文件中加上fastjson 依赖

compile 'com.alibaba:fastjson:1.2.31'

其实springboot 整合fastjson 共有两种方法,现在就逐一说一下

  1. 启动类继承extends WebMvcConfigurerAdapter 并且覆盖方法configureMessageConverters
  2. 在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;

猜你喜欢

转载自blog.csdn.net/weixin_39526391/article/details/77620102