FastJson/spring boot: json输出
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输出的更多相关文章
- FastJson/spring boot: json输出方法二
1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...
- 在Spring Boot中输出REST资源
前面我们我们已经看了Spring Boot中的很多知识点了,也见识到Spring Boot带给我们的各种便利了,今天我们来看看针对在Spring Boot中输出REST资源这一需求,Spring Bo ...
- Spring Boot Json 之 Jackjson Fastjson
Json 是目前互联网应用使用最为广泛的信息交换格式之一.Spring Boot 内置了 Jackson .Json 在应用中主要体现在以下功能: 序列化 反序列化 字段格式化 验证自动化 目前长用的 ...
- spring boot json 首字母大小写问题解决方案
spring boot默认使用的json解析框架是jackson,对于.net转java的项目来说太坑了,首字母大写的属性会自动转为小写,然后前端就悲剧了,十几个属性的ViewModel增加几个Js ...
- spring boot 自己输出json数据
@RequestMapping("/json")public void json(HttpServletResponse response, Pager pager, TruckF ...
- Spring Boot系列之配置日志输出等级
我们都知道Spring boot 默认使用 logback作进行日志输出,那么 在配置Spring boot日志输出时有两种方式: 通过application.properties 配置文件的方式来配 ...
- Spring Boot 系列教程8-EasyUI-edatagrid扩展
edatagrid扩展组件 edatagrid组件是datagrid的扩展组件,增加了统一处理CRUD的功能,可以用在数据比较简单的页面. 使用的时候需要额外引入jquery.edatagrid.js ...
- Spring Boot 知识图谱
最近有意重新学习下SpringBoot知识,特地总结了SpringBoot的知识点,对于想要学习的人来说,够用. SpringBoot学习路径 第一部分:了解 Spring Boot Spring B ...
- Spring Boot提供的特性
一.导览 本文主要按以下模块介绍spring Boot(1.3.6.RELEASE)提供的特性. SpringApplication类 外部化配置 Profiles 日志 开发WEB应用 Securi ...
随机推荐
- .Net 获取前端传递的数据
1. DotNet MVC: form是用来获得表单提交的数据:querystring是用来获得标识在URL后面的所有返回的变量及其值. 比如常见的URL网页地址都有xxx.asp?pn=123456 ...
- 您好,前端使用https,后端使用https是会有冲突的情况,所以默认后端都是http 负载均衡即可管理证书,不需要在后端ECS上绑定证书。
您前端使用https,那么前端就是加密的,后端使用https就是会访问出现问题的,目前阿里云负载均衡默认的配置前端使用https,后端默认就是http,也是无法更改的. 前端使用https,目前只有一 ...
- Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转
小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...
- Python开发【Django】:Form组件
Form组件 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 创建Form类时,主要涉及到 [ ...
- Flask简介之简单应用
Flask 0.Flask简介 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收ht ...
- 爬虫之selenium使用
详细使用链接: 点击链接 selenium介绍: selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质 ...
- Git版本控制工具安装与配置
这里太多,我写在这里方便复制: sudo yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel curl-dev ...
- MySQL创建索引命令
MySQL索引类型 普通索引 创建索引的方式 -- 直接新建索引 CREATE INDEX indexName ON mytable(username(length)) -- 修改表结构新建索引 AL ...
- (21)纹理缓存(Texture Cache)
简介 纹理缓存是将纹理缓存起来方便之后的绘制工作.每一个缓存的图像的大小,颜色和区域范围都是可以被修改的.这些信息都是存储在内存中的,不用在每一次绘制的时候都发送给GPU. CCTextureCach ...
- ng-深度学习-课程笔记-15: 循环序列模型(Week1)
1 数学符号(Notation) $ x^{<1>}, x^{<2>}, ..., x^{<t>}, ..., x^{<q>} $ 表示一段输入序列x, ...