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. MongoDB Sort op eration used more than the maximum 33554432 bytes of RAM. Add an index, or speci fy a smaller limit.

    最近在获取mongodb某个集合的数据过程中,在进行排序的过程中报错,具体报错信息如下: Error: error: { , "errmsg" : "Executor e ...

  2. mybatis的statement的解析与加载(springboot)

    问题 mybatis的xml中的sql语句是启动时生成JDK代理类的时候就生成一次么 调用顺序链 解析xml配置 Reader reader = Resources.getResourceAsRead ...

  3. (剑指Offer)面试题3:二维数组中的查找

    题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路: 鉴于数组的规律 ...

  4. 强大的json工具:fastJson

    fastJson   FastJSON是一个很好的java开源json工具类库,相比其他同类的json类库,它的速度的确是fast,最快!但是文档做得不好,在应用前不得不亲测一些功能.   实际上其他 ...

  5. WPF 同一个程序 只允许 同时运行一个

            方法2  当程序已经运行了 再运行这个程序时,则显示当前这个窗体  http://code.3rbang.com/cshape-run-one/ VS2013附件:http://fil ...

  6. C# WebRequest处理Https请求

    http://www.cnblogs.com/youlechang123/archive/2013/03/23/2976630.html 正常情况下,处理https和http没有什么区别,如以下代码, ...

  7. 局域网连接打印机(Win10)

    局域网支持交换机和WIFI环境下进行连接(要求连上打印机的电脑已开启) 1.首先打开控制面板 2.硬件和声音 3.高级打印机设置 4.找到要连接的打印机,通过浏览(R) 添加局域网某台机器上的打印机, ...

  8. Java从零开始学二十一(集合List接口)

    一.List接口 List是Collection的子接口,里面可以保存各个重复的内容,此接口的定义如下: public interface List<E> extends Collecti ...

  9. 转:Python yield 使用浅析

    初学 Python 的开发者经常会发现很多 Python 函数中用到了 yield 关键字,然而,带有 yield 的函数执行流程却和普通函数不一样,yield 到底用来做什么,为什么要设计 yiel ...

  10. java面试第三天

    类和对象: 类:主观抽象,是对象的模板,可以实例化对象----具有相同属性和行为的对象的集合. 习惯上类的定义格式: package xxx; import xxx; public class Xxx ...