1.引入FastJson依赖包

 <!-- FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>

  

pom.xml参考

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.muyang</groupId>
<artifactId>boot1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>boot1</name>
<url>http://maven.apache.org</url> <!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<!--<version>2.0.1.RELEASE</version>-->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--
自动依赖parent里面的版本
<version></version>
-->
</dependency> <!-- dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency--> <!-- FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> </dependencies> <build>
<finalName>boot1</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build> </project>

  

2.App.java继承WebMvcConfigurerAdapter,然后复写configureMessageConverters方法,加入自定义的FastJson配置

/**
* 复写configureMessageConverters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// TODO Auto-generated method stub
//super.configureMessageConverters(converters); /*
* 1、需要先定义一个 convert 转换消息的对象;
* 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
* 3、在convert中添加配置信息.
* 4、将convert添加到converters当中.
*
*/ // 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
); //3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig); //4、将convert添加到converters当中.
converters.add(fastConverter);
}

  

APP.JAVA参考

package com.muyang.boot1;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; /**
* Hello world!
*
*/
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter
{
private static Logger logger = Logger.getLogger(App.class); /**
* 复写configureMessageConverters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// TODO Auto-generated method stub
//super.configureMessageConverters(converters); /*
* 1、需要先定义一个 convert 转换消息的对象;
* 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
* 3、在convert中添加配置信息.
* 4、将convert添加到converters当中.
*
*/ // 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
); //3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig); //4、将convert添加到converters当中.
converters.add(fastConverter);
} public static void main( String[] args )
{
//System.out.println( "Hello World!" );\
SpringApplication.run(App.class, args);
logger.info("--sprint-boot-");
} }

  

3.建立Demo.java类,用来创建json输出之前的对象,并创建new Date()日期格式化属性

JSONField(format="yyyy-MM-dd HH:mm")

private Date createTime

/**
* 格式化时间
*/
@JSONField(format="yyyy-MM-dd HH:mm")
private Date createTime; /**
* 不显示json
*/
@JSONField(serialize=false)
private String remarks;

  

Demo.java参考

package com.muyang.boot1;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class Demo {

	private int id;

	private String name;

	/**
* 格式化时间
*/
@JSONField(format="yyyy-MM-dd HH:mm")
private Date createTime; /**
* 不显示json
*/
@JSONField(serialize=false)
private String remarks; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public String getRemarks() {
return remarks;
} public void setRemarks(String remarks) {
this.remarks = remarks;
} }

  

4.HelloController.java控制器

普通输出

@RequestMapping(value="/hello")
public String hello()
{
return "hello";
}

  

json输出

/**
* charset解决中文乱码
**/
@RequestMapping(value="/getDemo", produces = "application/json; charset=utf-8")
public Demo getDemo()
{
Demo demo = new Demo();
demo.setId(1);
demo.setName("张三");
demo.setCreateTime(new Date());
demo.setRemarks("这是备注信息");
return demo;
}

  

HelloController.java参考

package com.muyang.boot1;

import java.util.Date;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @RequestMapping(value="/hello")
public String hello()
{
return "hello";
} @RequestMapping(value="/getDemo", produces = "application/json; charset=utf-8")
public Demo getDemo()
{
Demo demo = new Demo();
demo.setId(1);
demo.setName("张三");
demo.setCreateTime(new Date());
demo.setRemarks("这是备注信息");
return demo;
} }

  

FastJson/spring boot: json输出的更多相关文章

  1. FastJson/spring boot: json输出方法二

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

  2. 在Spring Boot中输出REST资源

    前面我们我们已经看了Spring Boot中的很多知识点了,也见识到Spring Boot带给我们的各种便利了,今天我们来看看针对在Spring Boot中输出REST资源这一需求,Spring Bo ...

  3. Spring Boot Json 之 Jackjson Fastjson

    Json 是目前互联网应用使用最为广泛的信息交换格式之一.Spring Boot 内置了 Jackson .Json 在应用中主要体现在以下功能: 序列化 反序列化 字段格式化 验证自动化 目前长用的 ...

  4. spring boot json 首字母大小写问题解决方案

     spring boot默认使用的json解析框架是jackson,对于.net转java的项目来说太坑了,首字母大写的属性会自动转为小写,然后前端就悲剧了,十几个属性的ViewModel增加几个Js ...

  5. spring boot 自己输出json数据

    @RequestMapping("/json")public void json(HttpServletResponse response, Pager pager, TruckF ...

  6. Spring Boot系列之配置日志输出等级

    我们都知道Spring boot 默认使用 logback作进行日志输出,那么 在配置Spring boot日志输出时有两种方式: 通过application.properties 配置文件的方式来配 ...

  7. Spring Boot 系列教程8-EasyUI-edatagrid扩展

    edatagrid扩展组件 edatagrid组件是datagrid的扩展组件,增加了统一处理CRUD的功能,可以用在数据比较简单的页面. 使用的时候需要额外引入jquery.edatagrid.js ...

  8. Spring Boot 知识图谱

    最近有意重新学习下SpringBoot知识,特地总结了SpringBoot的知识点,对于想要学习的人来说,够用. SpringBoot学习路径 第一部分:了解 Spring Boot Spring B ...

  9. Spring Boot提供的特性

    一.导览 本文主要按以下模块介绍spring Boot(1.3.6.RELEASE)提供的特性. SpringApplication类 外部化配置 Profiles 日志 开发WEB应用 Securi ...

随机推荐

  1. Linq初探

    1.什么是LINQ LINQ是语言集成查询(Language Integrated Query),这项技术是在.net 3.5就已经引入的技术,极大的方便了数据的查询,他可以支持数据库.XML.ADO ...

  2. MacBook鼠标指针乱窜/不受控制问题的解决方法

    用了快一年的MacBook Pro最近出现了奇怪的问题.出问题时,鼠标不受控制,屏幕上鼠标指针乱窜,还时不时自动点击,犹如电脑被人远程控制一般.不管是用trackpad还是用外接鼠标,都是同样问题.电 ...

  3. linux加载硬盘过程

    查看系统可用磁盘大小: [root@i-mbyar7df ~]# df -h Filesystem      Size  Used Avail Use% Mounted on /dev/sda1    ...

  4. Key Set---hud5363(快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5363 #include <iostream> #include <cstdlib&g ...

  5. iis启动&nbsp;服务无法在此时接受控制信息。&nbsp;(异常来自&nbsp;HRESULT:0x80070425)

    问题描述:每隔一段时间应用程序池就会自动停止,报错:服务无法在此时接受控制信息.(异常来自 HRESULT:0x80070425)   iis启动 服务无法在此时接受控制信息. (异常来自HRESUL ...

  6. 用Python实现的数据结构与算法:堆栈

    一.概述 堆栈(Stack)是一种后进先出(LIFO)的线性数据结构,对堆栈的插入和删除操作都只能在栈顶(top)进行. 二.ADT 堆栈ADT(抽象数据类型)一般提供以下接口: Stack() 创建 ...

  7. Mybatis分页查询与动态SQL

    一.Mybatis的分页查询 由于第一二节较为详细讲述了Mybatis的环境搭建,文件配置,SQL编写和Java代码实现,所以接下来的讲述都将只抽取关键代码和mapper文件中的关键sql,详细的流程 ...

  8. SQL Server System.Data.SqlClient.SqlException:已成功于服务器建立连接,但是在 登录前的握手期间发生错误

    一.错误描述 错误名称如上.整体错误如下: System.Data.EntityException 基础提供程序在Open上失败--> System.Data.SqlClient.SqlExce ...

  9. 前端学习笔记之CSS后代选择器、子元素选择器、相邻兄弟选择器区别与详解

    派生选择器用的很多,派生选择器具体包括为后代选择器.子元素选择器.相邻兄弟选择器,我们来理解一下他们之间的具体用法与区别. 1.css后代选择器语法:h1 em {color:red;} 表示的是从h ...

  10. BZOJ2209: [Jsoi2011]括号序列

    传送门 splay练习. 考虑把括号序列转化成类似于区间最大/最小值的情况. 显然我们可以知道括号序列消完的情况肯定是$a$个)和$b$个(,那么把这些括号全部合法化的代价显然就是$\frac{a+1 ...