Skip to content

Commit dd21e0b

Browse files
committed
使用FastJson作为默认消息转换器
1 parent 323b26f commit dd21e0b

File tree

10 files changed

+498
-0
lines changed

10 files changed

+498
-0
lines changed

README-FastJson.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# SpingBoot使用fastJson
2+
3+
## 引入依赖
4+
```aidl
5+
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
6+
<dependency>
7+
<groupId>com.alibaba</groupId>
8+
<artifactId>fastjson</artifactId>
9+
<version>1.2.29</version>
10+
</dependency>
11+
12+
```
13+
## FastJsonHttpMessageConverter
14+
fastJson主要通过FastJsonHttpMessageConverter和FastJsonHttpMessageConverter4来处理我们的HttpMessageconvert,
15+
这两个类都是对HttpMessageConverter的实现.其中前者支持Spring4.2以前的,后者支持Spring4.2之后的版本.
16+
17+
## 使用fastJson
18+
+ 1.首先我们继承FastJsonHttpMessageConverter4,进行一些自己需要的设置,可以通过FastJsonConfig这个类来设置相关属性
19+
```aidl
20+
public class FastJsonConverter extends FastJsonHttpMessageConverter4 {
21+
}
22+
```
23+
+ 2.我们在我们的启动类里面用@Bean注入HttpMessageConverters,这样我们就可以使用fastJson了
24+
```aidl
25+
@SpringBootApplication
26+
public class App {
27+
28+
@Bean
29+
HttpMessageConverters fastJsonHttpMessageConverters() {
30+
return new HttpMessageConverters(new FastJsonConverter());
31+
}
32+
33+
public static void main(String[] args) {
34+
SpringApplication.run(App.class, args);
35+
}
36+
}
37+
```
38+
39+
## HttpMessageConverter
40+
HttpMessageConverter这个类定义了消息转换的几个方法,里面最重要的两个方法就是read()和write()方法.这是我们
41+
最终需要调用的两个方法,我们再看Spring已经实现的几个类.
42+
+ 1.AbstractHttpMessageConverter,这个类虽然实现read()和write方法了,但又使用了两个抽象方法readInternal()和
43+
writeInternal(),这是留给我们去具体的实现细节.
44+
+ 2.GenericHttpMessageConverter这也是一个接口.
45+
46+
+ 3.AbstractGenericHttpMessageConverter继承了AbstractHttpMessageConverter并且实现了GenericHttpMessageConverter,
47+
这个类其实也什么也没干,然后又又一个自己的writeInternal()方法需要我们具体去实现.
48+
+ 4.AbstractGenericHttpMessageConverter4,它继承自AbstractGenericHttpMessageConverter,实现writeInternal()方法,这个就是具体对read()的实现了,
49+
然后实现了readInternal()方法,这个方法来自AbstractHttpMessageConverter.
50+
51+
## read()和write()方法
52+
这是HttpMessageconvert最核心的两个方法
53+
+ 1.read()方法用来处理我们接受到的请求信息,比如对参数进行解密,参数验签等,当然这些功能我们也可以用SpringMVC的
54+
拦截器来实现,在以后会做介绍.
55+
+ 2.write()用来处理我们返回的请求信息,比如对参数加密,参数加签名等.

SpringBoot-FastJson/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>SpringBoot-Study</artifactId>
7+
<groupId>groupId</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>groupId</groupId>
13+
<artifactId>SpringBoot-FastJson</artifactId>
14+
<packaging>jar</packaging>
15+
16+
<dependencyManagement>
17+
<dependencies>
18+
<dependency>
19+
<!-- Import dependency management from Spring Boot -->
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-dependencies</artifactId>
22+
<version>1.4.0.RELEASE</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
35+
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
36+
<dependency>
37+
<groupId>com.alibaba</groupId>
38+
<artifactId>fastjson</artifactId>
39+
<version>1.2.29</version>
40+
</dependency>
41+
42+
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
43+
<dependency>
44+
<groupId>commons-io</groupId>
45+
<artifactId>commons-io</artifactId>
46+
<version>2.5</version>
47+
</dependency>
48+
49+
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
<executions>
58+
<execution>
59+
<goals>
60+
<goal>repackage</goal>
61+
</goals>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com;
2+
3+
import com.lc.springBoot.converter.EncrypConverter;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
7+
import org.springframework.context.annotation.Bean;
8+
9+
/**
10+
* @author lsj <lishuijun1992@gmail.com>
11+
* @date 17-3-28
12+
*/
13+
@SpringBootApplication
14+
public class App {
15+
16+
// @Bean
17+
// HttpMessageConverters setFastJsonConverter() {
18+
// return new HttpMessageConverters(new FastJsonConverter());
19+
// }
20+
21+
@Bean
22+
HttpMessageConverters fastJsonHttpMessageConverters() {
23+
return new HttpMessageConverters(new EncrypConverter());
24+
}
25+
26+
public static void main(String[] args) {
27+
SpringApplication.run(App.class, args);
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.lc.springBoot.controller;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.lc.springBoot.model.User;
5+
import org.springframework.web.bind.annotation.RequestBody;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* @author lsj <lishuijun1992@gmail.com>
11+
* @date 17-3-28
12+
*/
13+
@RestController
14+
@RequestMapping("user")
15+
public class UserController {
16+
17+
@RequestMapping("/getUser")
18+
public User getUser() {
19+
User user = new User();
20+
user.setName("test");
21+
user.setEmail("test@123.com");
22+
user.setAge(25);
23+
return user;
24+
}
25+
26+
@RequestMapping("/createUser")
27+
public String getcreateUserUser(@RequestBody User user) {
28+
System.out.println(JSON.toJSON(user));
29+
return "保存成功";
30+
}
31+
32+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.lc.springBoot.converter;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
5+
import com.lc.springBoot.util.Base64;
6+
import com.lc.springBoot.util.HttpBody;
7+
import org.apache.commons.io.IOUtils;
8+
import org.springframework.http.HttpInputMessage;
9+
import org.springframework.http.HttpOutputMessage;
10+
import org.springframework.http.converter.HttpMessageNotReadableException;
11+
import org.springframework.http.converter.HttpMessageNotWritableException;
12+
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.lang.reflect.Type;
16+
17+
/**
18+
* 实现参数加密解密
19+
* 我们参照FastJsonHttpMessageConverter4的实现,进行改造
20+
*
21+
* @author lsj <lishuijun1992@gmail.com>
22+
* @date 17-3-29
23+
*/
24+
public class EncrypConverter extends FastJsonHttpMessageConverter4 {
25+
26+
/**
27+
* 重写read()方法
28+
*
29+
* @param type
30+
* @param contextClass
31+
* @param inputMessage
32+
* @return
33+
* @throws IOException
34+
* @throws HttpMessageNotReadableException
35+
*/
36+
@Override
37+
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
38+
InputStream in = inputMessage.getBody();
39+
byte[] data = Base64.decode(IOUtils.toString(in, "UTF-8"));
40+
return JSON.parseObject(data, 0, data.length, getFastJsonConfig().getCharset(), type, getFastJsonConfig().getFeatures());
41+
}
42+
43+
/**
44+
* 重写readInternal
45+
*
46+
* @param clazz
47+
* @param inputMessage
48+
* @return
49+
* @throws IOException
50+
* @throws HttpMessageNotReadableException
51+
*/
52+
@Override
53+
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
54+
55+
InputStream in = inputMessage.getBody();
56+
byte[] data = Base64.decode(IOUtils.toString(in, "UTF-8"));
57+
return JSON.parseObject(data, 0, data.length, getFastJsonConfig().getCharset(), clazz, getFastJsonConfig().getFeatures());
58+
}
59+
60+
61+
/**
62+
* 重写writeInternal()方法
63+
*
64+
* @param obj
65+
* @param type
66+
* @param outputMessage
67+
* @throws IOException
68+
* @throws HttpMessageNotWritableException
69+
*/
70+
@Override
71+
protected void writeInternal(Object obj, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
72+
String s = Base64.encode(JSON.toJSONBytes(obj));
73+
HttpBody httpBody = new HttpBody();
74+
httpBody.setStatus(1);
75+
httpBody.setBody(s);
76+
super.writeInternal(httpBody, type, outputMessage);
77+
}
78+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.lc.springBoot.converter;
2+
3+
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
4+
5+
/**
6+
* @author lsj <lishuijun1992@gmail.com>
7+
* @date 17-4-3
8+
*/
9+
public class FastJsonConverter extends FastJsonHttpMessageConverter4 {
10+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.lc.springBoot.model;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* @author lsj <lishuijun1992@gmail.com>
9+
* @date 17-3-28
10+
*/
11+
public class User implements Serializable{
12+
// @JSONField(serialize = false)
13+
private String name;
14+
private int age;
15+
private String email;
16+
private List<User> friends;
17+
18+
public User(String name, int age, String email) {
19+
this.name = name;
20+
this.age = age;
21+
this.email = email;
22+
}
23+
24+
public User() {
25+
friends = new ArrayList<User>();
26+
friends.add(new User("lc1", 25, "123@123.test"));
27+
friends.add(new User("lc2", 25, "1234@123.test"));
28+
friends.add(new User("lc3", 25, "12345@123.test"));
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public int getAge() {
40+
return age;
41+
}
42+
43+
public void setAge(int age) {
44+
this.age = age;
45+
}
46+
47+
public String getEmail() {
48+
return email;
49+
}
50+
51+
public void setEmail(String email) {
52+
this.email = email;
53+
}
54+
55+
public List<User> getFriends() {
56+
return friends;
57+
}
58+
59+
public void setFriends(List<User> friends) {
60+
this.friends = friends;
61+
}
62+
}

0 commit comments

Comments
 (0)