第十讲 SpringBoot集成FastJson
文章目录
- 第十讲 SpringBoot集成FastJson
- 1.FastJson简介:
- 2. 引入依赖:pom.xml
- 3. 配置FastJson:FastJsonConfiguration
- 4. 编写Entity:
- 5. 编写Controller层:
- 6. 测试:访问 http://localhost:8080/users
1.FastJson简介:
Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协议交互、Web输出、Android客户端等多种应用场景
2. 引入依赖:pom.xml
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.31</version>
</dependency>
3. 配置FastJson:FastJsonConfiguration
package com.springboot.fastjson.config;import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.util.ArrayList;
import java.util.List;
&#64;Configuration
public class FastJsonConfiguration extends WebMvcConfigurerAdapter {&#64;Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {FastJsonHttpMessageConverter fastJsonHttpMessageConverter &#61; new FastJsonHttpMessageConverter();FastJsonConfig fastJsonConfig &#61; new FastJsonConfig();fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);List<MediaType> mediaTypes &#61; new ArrayList<>();mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);converters.add(fastJsonHttpMessageConverter);}
}
4. 编写Entity&#xff1a;
package com.springboot.fastjson.domain;
public class User {private Integer id;private String name;private String sex;public User() {super();}public User(Integer id, String name, String sex) {this.id &#61; id;this.name &#61; name;this.sex &#61; sex;}public Integer getId() {return id;}public void setId(Integer id) {this.id &#61; id;}public String getName() {return name;}public void setName(String name) {this.name &#61; name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex &#61; sex;}
}
5. 编写Controller层&#xff1a;
package com.springboot.fastjson.controller;import com.springboot.fastjson.domain.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;
&#64;RestController
public class UserController {&#64;RequestMapping("users")public List<User> getAllUser() {List<User> userList &#61; new ArrayList<User>();User user1 &#61; new User(1,"zhagnsan1","男");User user2 &#61; new User(2,"zhagnsan2","男");User user3 &#61; new User(3,"zhagnsan3","男");User user4 &#61; new User(4,"zhagnsan4","男");userList.add(user1);userList.add(user2);userList.add(user3);userList.add(user4);return userList;}&#64;RequestMapping("hello")public String syHello() {return "Hello World";}
}