IntelliJ IDEA 2017版开发SpringBoot之fastJsonHttpMessageConvert使用
继承WebMvcConfigurerAdapter,改写成自己的json转换工具的写法
1、建立实体类
package com.fastjson; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; /**
* Created by liuya on 2018-01-17.
*/
public class UserPoJo
{
private int userId;
private String userName;
@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date createTime; public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public int getUserId() {
return userId;
} public void setUserId(int userId) {
this.userId = userId;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} @Override
public String toString() {
return "UserPoJo{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", createTime=" + createTime +
'}';
}
}
注意:
@JSONField(format="yyyy-MM-dd HH:mm:ss")的使用是自定义格式
2、建立模拟服务器程序
package com.fastjson; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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; /**
* 模拟开启的服务器
*/ @SpringBootApplication
public class SpringboothelloApplication extends WebMvcConfigurerAdapter{ /**
// * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
// * @return
// */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //处理中文乱码
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes); //3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter;
converters.add(fastConverter);
} public static void main(String[] args) {
SpringApplication.run(SpringboothelloApplication.class, args);
} }
注意:测试程序测试前启动
3、测试用的Controller
package com.fastjson; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; /**
* Created by liuya on 2018-01-16.
*
* 测试用的一个helloworld例子
*/ @RestController
public class ControllerJson { @RequestMapping("user")
public UserPoJo hello(){
//实体类赋值
UserPoJo userPoJo = new UserPoJo();
userPoJo.setUserId(111);
userPoJo.setUserName("王小二");
userPoJo.setCreateTime(new Date());
//返回实体类
return userPoJo;
}
}
4、测试结果

5、遇到问题字符编码错误

6、解决在SpringboothelloApplication中加入如下代码解决
//处理中文乱码
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
IntelliJ IDEA 2017版开发SpringBoot之fastJsonHttpMessageConvert使用的更多相关文章
- IntelliJ IDEA 2017版 开发SpringBoot的全局配置文件使用
一.全局配置文件 描述: Spring Boot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径 ...
- Mac IntelliJ IDEA 2017(java开发集成环境)附注册码和破解教程 v2017.3.5破解版
原文:http://www.orsoon.com/Mac/155938.html 原文中含有软件下载地址 软件介绍 IntelliJ IDEA 2017 Mac激活版是Mac平台上的一款java开发集 ...
- IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理 (二)
第二部分 邮件发送历史 一.第一封邮件 1.1969年10月,世界上的第一封电子邮件 1969年10月世界上的第一封电子邮件是由计算机科学家Leonard K.教授发给他的同事的一条简短 ...
- IntelliJ IDEA 2017版 SpringBoot的web项目补充
一.注解 @SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置. @Configuration:这是一个配置Sprin ...
- IntelliJ IDEA 2017版 spring-boot基础补充,原理详解
一.Spring发展史 1.Spring1.x 版本一时代主要是通过XML文件配置bean,在java和xml中不断切换,在学习java web 初期的时候经常使用 2.Spring2 ...
- IntelliJ IDEA 2017版 spring-boot与Mybatis简单整合
一.编译器建立项目 参考:http://www.cnblogs.com/liuyangfirst/p/8372291.html 二.代码编辑 1.建立数据库 /* Navicat MySQL Data ...
- IntelliJ IDEA 2017版 spring-boot加载jsp配置详解(详细图文实例)
一.创建项目 (File--->New-->Project) 2.项目配置内容 3.选择配置项目的Group包名,Artifact项目名称 4.选择项目类型为web类型 5.创建成功,点击 ...
- IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)
一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...
- IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理 (一)
邮件发送功能总结 第一部分 背景 一.使用场景 (1)注册验证 注册各大网站,通常需要输入邮件地址,在注册成功后,会发送一封邮箱验证的邮件,点击确认,证明这个邮箱是用户自己的 ...
随机推荐
- SSH学习之四 OpenSSH安全
OpenSSH是Linux/Unix下一款加密通讯软件.同一时候也是我们用来远程控制Linux/Unixserver重要的必装软件. 对于各版本号的Linux及Unix发行版而言,O ...
- brave-zipkin的日志源码分析
其实在zipkin的日志里面作为发送端日志两个,sr,ss,这个日志是servlet产生的:接收端日志是四个,分别是cr,sr,ss,cs:cr和cs分别是上游的日志的信息:ss和sr是接收端输出的日 ...
- 02 - Unit010:关联映射
关联映射 什么是? 数据库中有关联关系的表,通过实体对象引用的方式体现出来,叫关联映射. 为什么? 将多表的记录封装成实体对象. 何时用? 对数据库中的表进行多表查询时. 怎么用? cn_user-- ...
- Java-Runoob-高级教程-实例-字符串:01. Java 实例 – 字符串比较
ylbtech-Java-Runoob-高级教程-实例-字符串:01. Java 实例 – 字符串比较 1.返回顶部 1. Java 实例 - 字符串比较 Java 实例 以下实例中我们通过字符串函 ...
- [html][javascript]父子窗体传值
父窗体 <script type="text/javascript"> newwindow = window.open("b1.html",&quo ...
- ThinkPHP实现事务回滚示例代码
ThinkPHP的事务回滚示例如下: ? 1 2 3 4 5 6 7 8 9 10 $m=D('YourModel');//或者是M(); $m2=D('YouModel2'); $m->sta ...
- 【转载】Python ConfigParser的使用
1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section ...
- 用jquery操作字体颜色覆盖当前页面的css设置
直接使用css操作color的时候,!important一直不生效,记录下,使用下面的可以起作用 用jquery操作字体颜色覆盖当前页面的css设置 $('a[href="?p=home&a ...
- 阻止后续事件的发生 onclick 双return 事件绑定(..$).click一个return
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- s2选择框的全选和反选jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...