-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathGsonSerializerConfigration.java
More file actions
39 lines (35 loc) · 1.46 KB
/
GsonSerializerConfigration.java
File metadata and controls
39 lines (35 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.spring4all.swagger;
import com.google.gson.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import springfox.documentation.spring.web.json.Json;
import java.lang.reflect.Type;
/**
* @Author: He Zhigang
* @Date: 2019/5/8 10:48
* @Description:
*/
@Slf4j
@Configuration
@ConditionalOnProperty(name = "spring.http.converters.preferred-json-mapper", havingValue = "gson")
public class GsonSerializerConfigration {
class SpringfoxJsonToGsonSerializer implements JsonSerializer<Json> {
@Override
public JsonElement serialize(Json json, Type type, JsonSerializationContext jsonSerializationContext) {
final JsonParser parser = new JsonParser();
return parser.parse(json.value());
}
}
@Bean
public GsonHttpMessageConverter gsonHttpMessageConverter() {
GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Json.class, new SpringfoxJsonToGsonSerializer());
log.info("注册 SpringfoxJsonToGsonSerializer TypeAdapter");
gsonHttpMessageConverter.setGson(builder.create());
return gsonHttpMessageConverter;
}
}