继承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使用的更多相关文章

  1. IntelliJ IDEA 2017版 开发SpringBoot的全局配置文件使用

    一.全局配置文件 描述:     Spring Boot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径 ...

  2. Mac IntelliJ IDEA 2017(java开发集成环境)附注册码和破解教程 v2017.3.5破解版

    原文:http://www.orsoon.com/Mac/155938.html 原文中含有软件下载地址 软件介绍 IntelliJ IDEA 2017 Mac激活版是Mac平台上的一款java开发集 ...

  3. IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理 (二)

    第二部分 邮件发送历史   一.第一封邮件   1.1969年10月,世界上的第一封电子邮件    1969年10月世界上的第一封电子邮件是由计算机科学家Leonard K.教授发给他的同事的一条简短 ...

  4. IntelliJ IDEA 2017版 SpringBoot的web项目补充

    一.注解        @SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置.        @Configuration:这是一个配置Sprin ...

  5. IntelliJ IDEA 2017版 spring-boot基础补充,原理详解

    一.Spring发展史  1.Spring1.x       版本一时代主要是通过XML文件配置bean,在java和xml中不断切换,在学习java web 初期的时候经常使用  2.Spring2 ...

  6. IntelliJ IDEA 2017版 spring-boot与Mybatis简单整合

    一.编译器建立项目 参考:http://www.cnblogs.com/liuyangfirst/p/8372291.html 二.代码编辑 1.建立数据库 /* Navicat MySQL Data ...

  7. IntelliJ IDEA 2017版 spring-boot加载jsp配置详解(详细图文实例)

    一.创建项目 (File--->New-->Project) 2.项目配置内容 3.选择配置项目的Group包名,Artifact项目名称 4.选择项目类型为web类型 5.创建成功,点击 ...

  8. IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)

    一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...

  9. IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理 (一)

    邮件发送功能总结        第一部分 背景   一.使用场景 (1)注册验证    注册各大网站,通常需要输入邮件地址,在注册成功后,会发送一封邮箱验证的邮件,点击确认,证明这个邮箱是用户自己的 ...

随机推荐

  1. graphql 文档 docker 镜像

    因为一些原因 graphql 的官方文档无法查看,后者查看不能是方便,所以在官方github 的文档基础上添加了容器构建, 方便进行查看,对于公司内部使用学习会比较好 原理 很简单,openresty ...

  2. 为已编译的DLL附带强命名

    在我们开发的过程中,会经常调用其他人写好的DLL类库,由于种种的原因,不管是公司规定,还是个人习惯等等的原因,有时候需要调用各个类库直接邀请必须强命名. 但是我们临时也无法找到源代码进行重新编译等事情 ...

  3. selenium+python 移动鼠标方法

    from selenium import webdriver from selenium.webdriver.common.keys import Keys import time driver=we ...

  4. MySQL 数据库查询练习

    -- ---------------------------- -- mysql练习sql脚本 -- ---------------------------- create database db10 ...

  5. [转][C#]压缩解压缩类 GZipStream

    本文来自:https://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream(v=vs.100).aspx using ...

  6. Java 8 : Stream API 练习

    //店铺属性类 public class Property { String name; // 距离,单位:米 Integer distance; // 销量,月售 Integer sales; // ...

  7. 第七章 : Git 介绍 (下)[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 Let’s reset even further to remove all traces of your work on the deprecat ...

  8. leetcode917

    class Solution { public: string reverseOnlyLetters(string S) { int len = S.length(); queue<char&g ...

  9. Firemonkey MultiView

    MultiView 做导航用的. http://docwiki.embarcadero.com/RADStudio/Seattle/en/Mobile_Tutorial:_Using_a_MultiV ...

  10. 0 1 1 2 3 5 8 13 21 34 求第N个, 用js实现

    function fibo(n) { var f = []; for (var c = 0; c < n; ++c) { console.log(f.join("")) f. ...