Spring Boot返回json数据

视频地址:http://www.iqiyi.com/w_19rubxzsr5.html

博文参考:https://blog.csdn.net/linxingliang/article/details/51582294

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

 

视频地址: http://baidu.iqiyi.com/watch/0669833408793393358.html

博文参考:
https://my.oschina.net/sdlvzg/blog/1153921
http://412887952-qq-com.iteye.com/blog/2413390

代码备份:

App.java

package edu.shy.javaee;

import java.util.ArrayList;
import java.util.List; 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.MediaType;
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; //extends WebMvcConfigurerAdapter @SpringBootApplication
public class App{ // @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//
// super.configureMessageConverters(converters);
// //1、定义一个convert转换消息的对象
// FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// //2、添加fastjson的配置信息
// FastJsonConfig fastJsonConfig = new FastJsonConfig();
// fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// //2-1 处理中文乱码问题
// List<MediaType> fastMediaTypes = new ArrayList<>();
// fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
// fastConverter.setSupportedMediaTypes(fastMediaTypes);
// //3、在convert中添加配置信息
// fastConverter.setFastJsonConfig(fastJsonConfig);
// //4、将convert添加到converters中
// converters.add(fastConverter);
// } //引入Fastjson解析json,不使用默认的jackson
//必须在pom.xml引入fastjson的jar包,并且版必须大于1.2.10
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
//1、定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//2-1 处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters中
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
} /**
* @param args
* @throws Exception
*/
public static void main( String[] args ) throws Exception
{
SpringApplication.run(App.class,args);
}
}

JsonDemo.java

package edu.shy.javaee;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

/**
* 这是一个测试实体类
* @author yansixing
* @date 2018年5月10日 下午2:43:26
*/
public class JsonDemo {
private int id; // 主键
private String name; // 测试名称 @JSONField(serialize=false) //转为JSON时不包括此属性
private String remark; //备注 @JSONField(format="yyyy-MM-dd HH:mm") //转为JSON时按格式转换
private Date createTime; public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = 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;
}
}

HelloController.java

package edu.shy.javaee;

import java.util.Date;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 在这里我们使用RestController (等价于@Controller和@RequestBody)
* @author yansixing
* @date 2018年5月9日 下午3:46:20
*/
/**
* @author yansixing
* @date 2018年5月10日 下午3:14:52
*/
/**
* @author yansixing
* @date 2018年5月10日 下午3:14:54
*/
/**
* @author yansixing
* @date 2018年5月10日 下午3:14:58
*/
@RestController
@EnableAutoConfiguration
public class HelloController { @RequestMapping("/")
String home(){
return "Hello Spring Boot!";
} @RequestMapping("/hello/{myName}")
String index(@PathVariable String myName){
return "Hello "+myName+"!!!";
} /**
* Spring Boot默认使用的Json解析框架是Jackson
* 请求地址:http://127.0.0.1:8089/getJson
* @return
*/
@RequestMapping("/getJson")
public JsonDemo getJson(){
JsonDemo demo=new JsonDemo();
demo.setId();
demo.setName("张三");
demo.setCreateTime(new Date());
demo.setRemark("remark");
return demo;
}
}

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.</modelVersion> <groupId>edu.shy.javaee</groupId>
<artifactId>sprint-boot-hello</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <name>sprint-boot-hello</name>
<url>http://maven.apache.org</url>
<!--sprint boot 父节点依赖,引入这个之后相关的引入就不需要加version配置,sprint boot 会自动选择最合适的版本进行添加 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4..BUILD-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<!-- 指定一下jdk的版本,这里我们 使用jdk1.,默认1. -->
<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>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<!-- 改变spring boot 默认的json解析,通过引入fastjson改变 -->
<!-- fastjson版本必须 大于1.2.10 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency> </dependencies> <!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories> </project>

Spring Boot返回json数据及完美使用FastJson解析Json数据的更多相关文章

  1. 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...

  2. Spring Boot 返回 XML 数据,一分钟搞定!

    Spring Boot 返回 XML 数据,前提必须已经搭建了 Spring Boot 项目,所以这一块代码就不贴了,可以点击查看之前分享的 Spring Boot 返回 JSON 数据,一分钟搞定! ...

  3. 【转】Spring Boot干货系列:(二)配置文件解析

    转自:Spring Boot干货系列:(二)配置文件解析 前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用"习惯优于配置"(项目中存在大量的配置,此 ...

  4. Spring Boot干货系列:(二)配置文件解析

    Spring Boot干货系列:(二)配置文件解析 2017-02-28 嘟嘟MD 嘟爷java超神学堂   前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用“习惯优于 ...

  5. fastjson解析json数组

    1.fastjson解析json数组(直接上代码) import java.util.ArrayList; import java.util.List; import com.alibaba.fast ...

  6. JavaWeb_(Jar)使用fastjson解析json和序列化对象

    菜鸟教程 传送门 JSON官网 传送门 fastjson插件下载 传送门 序列化[百度百科]:序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对 ...

  7. FastJson解析Json,封装JavaBean对象

    获取到前端的Json,后台对应封装JavaBean对象,对其解析赋值 获取到前端的json,对其进行分析 1.获取最外层前端json对应得JavaBean (1)未分析格式的json串 (2)初步格式 ...

  8. Scala中使用fastJson 解析json字符串

    Scala中使用fastJson 解析json字符串 添加依赖 2.解析json字符 2.1可以通过JSON中的parseObject方法,把json字符转转换为一个JSONObject对象 2.2然 ...

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

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

随机推荐

  1. COM不同的线程模型对列集和同步的不同要求。

  2. 防盗链Nginx设置图片防盗链,设置无效的请仔细看红字

    *******************************************************************切记,替换的图片地址要使用没有防盗链的网站图片,否则由于替换的图片 ...

  3. Exception in thread "main" java.lang.ClassCastException: $Proxy13

    Exception in thread "main" java.lang.ClassCastException: $Proxy13原因:业务层实现了接口 解决:方法一:切面配置事务 ...

  4. [置顶] macbook 深度休眠和待机

    开发用了macbook pro, 10.8.3, 因为用windows的习惯,一直比较习惯不关机,直接休眠,不是待机standby,今天找到了一个工具,可以实现,亲测通过. 下载:https://gi ...

  5. MemSQL学习笔记-类似MySQL的数据库

    http://gigaom.com/cloud/ex-facebookers-launch-memsql-to-make-your-database-fly/ -- 多主-从 http://www.m ...

  6. ASP.NET之旅--深入浅出解读IIS架构

    在学习Asp.net时,发现大多数作者都是站在一个比较高的层次上讲解Asp.Net. 他们耐心. 细致地告诉你如何一步步拖放控件. 设置控件属性.编写 CodeBehind代码,以实现某个特定的功能. ...

  7. php之快速入门学习-12(超级全局变量)

    PHP 超级全局变量 超级全局变量在PHP 4.1.0之后被启用, 是PHP系统中自带的变量,在一个脚本的全部作用域中都可用. PHP 超级全局变量 PHP中预定义了几个超级全局变量(superglo ...

  8. java面试第六天

    集合:保存多个其他对象的对象,不能保存简单类型 List:有序(存放元素的顺序),可重复的集合 ArrayList:实质就是一个会自动增长的数组 查询效率比较高,增删的效率比较低,适用于查询比较频繁, ...

  9. Fail Fast and Fail Safe Iterators in Java

    https://www.geeksforgeeks.org/fail-fast-fail-safe-iterators-java/ Fail Fast and Fail Safe Iterators ...

  10. SwaggerUI+SpringMVC——构建RestFul API的可视化界面

    今天给大家介绍一款工具,这个工具眼下可预见的优点是:自己主动维护最新的接口文档. 我们都知道,接口文档是非常重要的,可是随着代码的不断更新,文档却非常难持续跟着更新,今天要介绍的工具,完美的攻克了这个 ...