加入maven 依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.45</version> </dependency>
创建HttpMessageConverters ,这里是无XML方式创建
import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @Configuration public class BeanConfig { @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); config.setSerializerFeatures(); converter.setFastJsonConfig(config); converter.setDefaultCharset(Charset.forName("UTF-8")); // 设置编码 return new HttpMessageConverters(converter); } }如果传输Bean 对象 可以在字段上使用 @JSONField 注解来制定返回json 对象
@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date createTime;
@JSONField(serialize=false)