1.步骤:

1. 编写实体类Demo

2. 编写getDemo()方法

3. 测试

2.项目构建

编写实体类Demo

package com.kfit;

/**
* 这是一个测试实体类.
*/
public class Demo {
private int id;
private String name;
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;
} }

编写getDemo()

package com.kfit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 在这里我们使用RestController (等待于 @Controller 和 @RequestBody)
*/
@RestController
public class HelloController { /**
* 在这里我们使用@RequestMapping 建立请求映射:
* http://127.0.0.1:8080/hello
* @return
*/
@RequestMapping("/hello")
public String hello(){
return "hello-2016-12-11.v.0";
}
/**
* Spring Boot默认使用的json解析框架是jackson
* http://127.0.0.1:8080/getDemo
* @return
*/
@RequestMapping("/getDemo")
public Demo getDemo(){
Demo demo = new Demo();
demo.setId(1);
demo.setName("张三");
return demo;
}
}

Spring Boot完美使用FastJson解析JSON数据

个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来比较不习惯,所以很自然我就想我能不能使用fastjson进行json解析

引入fastjson依赖库

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>1.2.15</version>

</dependency>

这里要说下很重要的话,官方文档说的1.2.10以后,

会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,

4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,

具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,

所以这里最低要求就是1.2.10+。

配置fastjon(支持两种方法)

第一种方法就是:

(1)启动类继承extends WebMvcConfigurerAdapter

(2)覆盖方法configureMessageConverters

代码

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.kfit</groupId>
<artifactId>spring-boot-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-boot-hello</name>
<url>http://maven.apache.org</url> <!--
spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
<java.version>1.8</java.version>
</properties> <dependencies>
<!--
spring-boot-starter-web: MVC,AOP的依赖包....
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--
<version></version>
由于我们在上面指定了 parent(spring boot)
-->
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency> </dependencies>
</project>

启动类

package com.kfit;

import java.util.List;

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; /**
* 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.
*/
//extends WebMvcConfigurerAdapter
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter{ @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
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) {
/*
* 在main方法进行启动我们的应用程序.
*/
SpringApplication.run(App.class, args);
}
}

实体类

package com.kfit;

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;
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;
} }

controller类

package com.kfit;
import java.util.Date; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 在这里我们使用RestController (等待于 @Controller 和 @RequestBody)
*/
@RestController
public class HelloController { /**
* 在这里我们使用@RequestMapping 建立请求映射:
* http://127.0.0.1:8080/hello
* @return
*/
@RequestMapping("/hello")
public String hello(){
return "hello-2016-12-11.v.0";
}
/**
* Spring Boot默认使用的json解析框架是jackson
* http://127.0.0.1:8080/getDemo
* @return
*/
@RequestMapping("/getDemo")
public Demo getDemo(){
Demo demo = new Demo();
demo.setId(1);
demo.setName("张三");
demo.setCreateTime(new Date());
return demo;
}
}

第二种方法

1)在App.java启动类中, 注入Bean : HttpMessageConverters

package com.kfit;

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指定这是一个 spring boot的应用程序.
*/ @SpringBootApplication
public class App{
/**
* 在这里我们使用 @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);
} /**
* @param args
*/
public static void main(String[] args) {
/*
* 在main方法进行启动我们的应用程序.
*/
SpringApplication.run(App.class, args);
}
}

总结

我们要使用第三方的json解析框架的话:
1、我们需要在pom.xml中引入相应的依赖;
2、需要在App.java中继承WebMvcConfigurerAdapter重写方法:configureMessageConverters 添加我们自己定义的json解析框架;
2.1 @Bean注入第三方的json解析框架:

Spring boot之返回json数据的更多相关文章

  1. Spring MVC中返回JSON数据的几种方式

    我们都知道Spring MVC 的Controller方法中默认可以返回ModeAndView 和String 类型,返回的这两种类型数据是被DispatcherServlet拿来给到视图解析器进行继 ...

  2. 【spring】 SpringMVC返回json数据的三种方式

    配置方法一 **1.导入第三方的jackson包,jackson-mapper-asl-1.9.7.jar和jackson-core-asl-1.9.7.jar. 2.spring配置文件添加** & ...

  3. spring MVC之返回JSON数据(Spring3.0 MVC)

    方式一:使用ModelAndView的contentType是"application/json" 方式二:返回String的            contentType是&qu ...

  4. spring boot 自己输出json数据

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

  5. Spring Boot 返回 JSON 数据,一分钟搞定!

    如何返回 JSON 数据? 在 Spring Boot 中返回 JSON 数据很简单,如下几步. 加入依赖 12345678910 <parent> <groupId>org. ...

  6. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  7. 2. Spring Boot返回json数据【从零开始学Spring Boot】

    在做如下操作之前,我们对之前的Hello进行简单的修改,我们新建一个包com.kfit.test.web然后新建一个类HelloControoler,然后修改App.Java类,主要是的这个类就是一个 ...

  8. (2)Spring Boot返回json数据【从零开始学Spring Boot】

    在做如下操作之前,我们对之前的Hello进行简单的修改,我们新建一个包com.kfit.test.web 然后新建一个类HelloControoler, 然后修改App.java类,主要是的这个类就是 ...

  9. Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理

    Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理 今天在使用spring4.1.4,使用ResponseBody注解返回JSON格式的数据的时候遇到406错误. 解决 ...

随机推荐

  1. python网络爬虫(5)BeautifulSoup的使用示范

    创建并显示原始内容 其中的lxml第三方解释器加快解析速度 import bs4 from bs4 import BeautifulSoup html_str = """ ...

  2. redis 学习(9)-- redis 客户端 -- redis-py

    redis 客户端 -- redis-py 简介 关于 redis 的各种客户端,我们可以在官网上寻找并使用,比如我这里的 python 客户端,可以在官网上找到:redis-client . 获取 ...

  3. TCP 三次握手和四次挥手

    TCP 三次握手和四次挥手 作为面试会被经常考察的的点,自己复习了一下,总结如下: TCP 三次握手 先上图: 所谓三次握手,是指建立一个 TCP 连接时,需要客户端和服务器总共发送 3 个包. 第一 ...

  4. 2018年4月份,阿里最新的java程序员面试题目,仅供参考。

    目录 技术一面(23问) 技术二面(3大块) 性能优化(21点) 项目实战(34块) JAVA方向技术考察点(15点) JAVA开发技术面试中可能问到的问题(17问) 阿里技术面试1 1.Java I ...

  5. API接口之安全篇

    APP.前后端分离项目都采用API接口形式与服务器进行数据通信,传输的数据被偷窥.被抓包.被伪造时有发生,那么如何设计一套比较安全的API接口方案呢? 一般的解决方案如下: 1.Token授权认证,防 ...

  6. 使用 flask-restful 编写 自己的 ai web service

    本项目在 win 平台采用 pycharm 编写, 技能与环境要求: python 基础, web 基础知识, python.exe = 3.6+ 算法>第四版,操作系统推荐<现代操作系统 ...

  7. 2019.9.19HTML基础

    html:超文本标记语言,不是编程语言,是标签语言,显示数据. 有双标签和单标签 双标签:有开始有结束,<body></body> 单标签:只有一个.<img src=# ...

  8. 对于国嵌上学期《一跃进入C大门》Mini2440的代码修正

    摸索了几天,加了无数的群,病急乱投医式地问了好多个人,终于改对了代码. 下面先贴出给的范例代码 这是C语言代码,是没有错的. 那么出错的地方就在start.S部分 很明显,MPLLCON地址错误,正确 ...

  9. 第三方库-时间函数dateutil

    在dateutil中,吸引我的东西有2个,1个是parser,1个是rrule. 其中parser是根据字符串解析成datetime,而rrule是则是根据定义的规则来生成datetime. 安装没必 ...

  10. zencart只有购买过此产品的客户才能评价产品

    当前登录的客户买过此产品时,才显示评价按钮: <?php $rev_query = "select count(*) as count from orders o ,orders_pr ...