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.Demo.java对象参加

引用FastJson中的额JSONField(format="yyyy-MM-dd HH:mm")格式化new Date()日期

/**
* 格式化时间
*/
@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;
} }

  

3.创建App2.java启动器,同时配置FastJson配置

/**
* 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters()
{
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
); //3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}

  

App2.java参考

package com.muyang.boot1;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication
public class App2 { private static Logger logger = Logger.getLogger(App2.class); /**
* 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters()
{
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
); //3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
} public static void main(String[] args)
{
SpringApplication.run(App2.class, args);
} }

  

4.创建HelloController.java控制器

普通输出

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

  

json输出

@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. Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  4. Spring Boot 2.X(十二):定时任务

    简介 定时任务是后端开发中常见的需求,主要应用场景有定期数据报表.定时消息通知.异步的后台业务逻辑处理.日志分析处理.垃圾数据清理.定时更新缓存等等. Spring Boot 集成了一整套的定时任务工 ...

  5. 这些保护Spring Boot 应用的方法,你都用了吗?

    这些保护Spring Boot 应用的方法,你都用了吗? 生如夏花 SpringForAll社区 今天 Spring Boot大大简化了Spring应用程序的开发.它的自动配置和启动依赖大大减少了开始 ...

  6. Spring Boot Json 之 Jackjson Fastjson

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

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

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

  8. Spring Boot入门系列(二十六)超级简单!Spring Data JPA 的使用!

    之前介绍了Mybatis数据库ORM框架,也介绍了使用Spring Boot 的jdbcTemplate 操作数据库.其实Spring Boot 还有一个非常实用的数据操作框架:Spring Data ...

  9. Spring Boot快速入门(二):http请求

    原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...

随机推荐

  1. python range函数与numpy arange函数

    1.range()返回的是range object,而np.arange()返回的是numpy.ndarray() range尽可用于迭代,而np.arange作用远不止于此,它是一个序列,可被当做向 ...

  2. postgresql----字符串函数与操作符

    函数 返回值类型 描述 示例 结果 string||string text 字符串连接 select 'Post'||'gresql'||' good!'; Postgresql good! stri ...

  3. C# get post 的方法

    #region GET POST /// <summary> /// Get String data = GetString(URL , "PKEY=" + Pkeyl ...

  4. 数据去重优化 MemoryError 内存不足

    from ProjectUtil.usingModuleTOMODIFY import getNow export_q_f, q_l, start_ = '/mnt/mongoexport/super ...

  5. 过山车---hdu2063(最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063最大匹配模板题: #include <iostream> #include <c ...

  6. SQL基础--查询之五--查询语句一般格式

    SQL基础--查询之五--查询语句一般格式

  7. Nginx rewrite 中break与last指令的区别

    location /break/ { rewrite ^/break/(.*) /test/$1 break; return 402; } location /last/ { rewrite ^/la ...

  8. (2.12)Mysql之SQL基础——存储过程条件定义与错误处理

    转自:博客园桦仔 5.存储过程条件定义与错误处理 -- (1)定义 [1]条件定义:declare condition_name condition for condition_value;[2]错误 ...

  9. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  10. IETF国标查询

    IETF官网 https://www.ietf.org/ rfc国标官网 https://www.ietf.org/standards/rfcs/ rfc国标查询 https://www.rfc-ed ...