Spring Boot 初体验——FastJson
Posted kuangyefeige
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 初体验——FastJson相关的知识,希望对你有一定的参考价值。
1.pom文件中加入fastJson依赖版本要求 1.2.10+
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
2。修改启动类的方法
方法一:
(1)启动类继承extends WebMvcConfigurerAdapter
(2)覆盖方法configureMessageConverters
package com.mt; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class App extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters( List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); FastJsonHttpMessageConverter convvert = new FastJsonHttpMessageConverter(); FastJsonConfig jsonConfig = new FastJsonConfig(); jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); convvert.setFastJsonConfig(jsonConfig); converters.add(convvert); } public static void main(String[] args) { System.out.println("helloSpring Boot"); SpringApplication.run(App.class, args); } }
方法二:
(1)在App.java启动类中, 注入Bean : HttpMessageConverters
package com.mt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.converter.HttpMessageConverter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class App /* extends WebMvcConfigurerAdapter */ { /* * @Override public void configureMessageConverters( * List<HttpMessageConverter<?>> converters) { * super.configureMessageConverters(converters); * FastJsonHttpMessageConverter convvert = new * FastJsonHttpMessageConverter(); FastJsonConfig jsonConfig = new * FastJsonConfig(); * jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); * convvert.setFastJsonConfig(jsonConfig); converters.add(convvert); * * } */ public static void main(String[] args) { System.out.println("helloSpring Boot"); SpringApplication.run(App.class, args); } // 方法二: @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } }
3.使用fastjson实体类加注解使用Fastjson
package com.mt.pojo; import java.util.Date; import com.alibaba.fastjson.annotation.JSONField; public class User { private Long id; private String name; private String pwd; @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date creatTime; public User(Long id, String name, String pwd, Date creatTime) { super(); this.id = id; this.name = name; this.pwd = pwd; this.creatTime = creatTime; } public Date getCreatTime() { return creatTime; } public void setCreatTime(Date creatTime) { this.creatTime = creatTime; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public User(Long id, String name, String pwd) { super(); this.id = id; this.name = name; this.pwd = pwd; } public User() { super(); } }
4.启动测试效果
以上是关于Spring Boot 初体验——FastJson的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 初体验(10)使用使用freemarker
215.Spring Boot+Spring Security:初体验
215.Spring Boot+Spring Security:初体验
小马哥-Java 微服务实践 - Spring Boot 系列-01Java 微服务实践 - Spring Boot 系列初体验