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)注册验证 注册各大网站,通常需要输入邮件地址,在注册成功后,会发送一封邮箱验证的邮件,点击确认,证明这个邮箱是用户自己的 ...
随机推荐
- nexus docker 私有镜像处理
新版本的nexus 可以进行docker 镜像的存储处理 配置私有镜像(host 模式) 修改docker 非安全镜像处理 { "registry-mirrors": [" ...
- UOJ 348 【WC2018】州区划分——子集卷积
题目:http://uoj.ac/problem/348 参考:https://www.cnblogs.com/NaVi-Awson/p/9242645.html#%E5%AD%90%E9%9B%86 ...
- WeX5 苹果APP打包教程
来源:http://docs.wex5.com/app-packing/ WeX5平台App打包教程 WeX5是前端快速开发框架,可开发跨端运行应用,是移动App/微信/WebApp开发利器,一次开发 ...
- 1027代码审计平台 2-sonarscanner项目变更
修改version,可以获得新版本的解析数据,与以往结果比对,获取bug.漏洞.代码不规范.覆盖率等变化,重点关注新增的bug及问题分布 version参数修改 1.对sonar-project.pr ...
- VMware vSphere Client下增加虚拟机磁盘空间的方法
随着系统运维时间的增长,磁盘就日益的损耗,如果遇到虚拟机报磁盘空间不足怎么办?还好,我们可以通过磁盘阵列增加磁盘空间,然后扩容到虚拟机中去. 对于linux虚拟机磁盘扩容的方案有两种,一种就是原有的实 ...
- id取模分表
场景 1 假设按用户id分2个库 每个库分10张表. 分表策略 1.用户id%2 确定库 用户id%3确定表. 2.(用户id%(2*10))/ 10 取整确定库,(用户id%(2*10)%10确 ...
- [Delphi] FMXUI - 沉侵式状态栏
好消息!FMXUI正式支持Android和IOS的沉侵式状态栏了. 先来看看效果图 安卓: IOS: 一.Android 沉侵式状态栏 FMXUI的沉侵式状态栏支持SDK版本大于21的Android系 ...
- 一次使用 Redis 优化查询性能的实践
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,一次使用 Redis 优化查询性能的实践 应用背景 有一个应用需要上传一组ID到 ...
- C++Builder 也有StringBuilder
System::Sysutils::TStringBuilder *sb = new TStringBuilder; sb->Append("ww"); sb-& ...
- ETL 脚本任务
Public Sub Main() ' ' Add your code here ' Dim sbWA As New StringBuilder ...