热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

第10讲SpringBoot集成FastJson

第十讲SpringBoot集成FastJson文章目录第十讲SpringBoot集成FastJson1.FastJson简介:2.引入依赖:pom.xm
第十讲 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;Description:* &#64;Author: zrblog* &#64;CreateTime: 2018-09-25 22:52* &#64;Version:v1.0*/
&#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;


  • User.java

package com.springboot.fastjson.domain;/*** &#64;Description:* &#64;Author: zrblog* &#64;CreateTime: 2018-09-25 23:05* &#64;Version:v1.0*/
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;Description:* &#64;Author: zrblog* &#64;CreateTime: 2018-09-25 23:04* &#64;Version:v1.0*/
&#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";}
}

6. 测试&#xff1a;访问 http://localhost:8080/users

a


推荐阅读
author-avatar
荆灬樊小姐_526
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有