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

 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>
上一篇:在windows系统上使用pip命令安装python的第三方库


下一篇:Windows AD日志分析平台WatchAD安装教程