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. 设计模式之十五:訪问者模式(Visitor Pattern)

    訪问者模式(Visitor Pattern)是GoF提出的23种设计模式中的一种,属于行为模式. 据<大话设计模式>中说算是最复杂也是最难以理解的一种模式了. 定义(源于GoF<De ...

  2. windows安装go-sqlite3失败,提示找不到gcc

    windows安装go-sqlite3失败,提示找不到gcc go get github.com/mattn/go-sqlite3时失败,提示exec: “gcc”: executable file ...

  3. iframe之onload事件小记

    项目上做了一个具有wizard(向导)功能的菜单导航页面,子页面的引入通过主页面上iframe的src属性切换实现.为了有个良好的交互体验,每次更新iframe的src时,主页面上都显示一个模态的lo ...

  4. [转]How to query posts filtered by custom field values

    Description It is often necessary to query the database for a list of posts based on a custom field ...

  5. [转载]C++中处理XML文件

      写Unmanaged Code在.NET时代成为一种很悲惨的事,当你需要处理XML文件时,这种感觉会变得尤其强烈.FCL中的System.XML多简单啊,连Steve Ballmer都知道怎么用. ...

  6. [Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure

    function buildCoveTicketMarker(transport){ var passengerNumber = 0; return function(name){ passenger ...

  7. 经典,HTML5游戏,超级玛丽

    在线演示 在线演示 本地下载 这是一款使用HTML5开发的超级玛丽,有没有点儿时的记忆?长按向上键,可以跳的更高哦.如果你也喜欢可以当成休闲游戏,如果你是开发者,不防下载下来看看是如何生成的.

  8. Eclipse 之使用技巧积累(一)

    1.代码缩进 选中缩进代码,然后点击“Tab”键增加缩进. 2.代码退格 选中退格代码,然后按住“Shift”键,再点击“Tab”键退格. 3.无格式代码格式化 (1)默认快捷键:Ctrl + Shi ...

  9. iOS new 和 alloc的区别

    一般如果只是 alloc init 那么 用new 是一样的 SomeObject*myObject =[[SomeObject alloc] init]; SomeObject*myObject = ...

  10. DOM与JavaScript、jQuery之间的关系

    DOM(document object model) 其实是浏览器内元素对象的一个总称 我们用JavaScript对网页进行的所有操作都是通过DOM进行的.DOM属于浏览器,而不是JavaScript ...