1
+ package com .mengxiang .im .push .provider ;
2
+
3
+ import com .google .gson .Gson ;
4
+ import com .google .gson .GsonBuilder ;
5
+ import com .google .gson .stream .JsonReader ;
6
+ import com .google .gson .stream .JsonWriter ;
7
+
8
+ import javax .ws .rs .Consumes ;
9
+ import javax .ws .rs .Produces ;
10
+ import javax .ws .rs .WebApplicationException ;
11
+ import javax .ws .rs .core .MediaType ;
12
+ import javax .ws .rs .core .MultivaluedMap ;
13
+ import javax .ws .rs .ext .MessageBodyReader ;
14
+ import javax .ws .rs .ext .MessageBodyWriter ;
15
+ import javax .ws .rs .ext .Provider ;
16
+ import java .io .*;
17
+ import java .lang .annotation .Annotation ;
18
+ import java .lang .reflect .Type ;
19
+ import java .nio .charset .Charset ;
20
+ import java .time .LocalDateTime ;
21
+
22
+ /**
23
+ * 用于设置Jersey的Json转换器
24
+ * 用于替换JacksonJsonProvider
25
+ * <p>
26
+ * 该工具类完成了,把Http请求中的请求数据转换为Model实体,
27
+ * 同时也实现了把返回的Model实体转换为Json字符串
28
+ * 并输出到Http的返回体中。
29
+ *
30
+ * @param <T> 任意类型范型定义
31
+ */
32
+ @ Provider
33
+ @ Produces (MediaType .APPLICATION_JSON )
34
+ @ Consumes (MediaType .APPLICATION_JSON )
35
+ public class GsonProvider <T > implements MessageBodyReader <T >, MessageBodyWriter <T > {
36
+ // 共用一个全局的Gson
37
+ private static final Gson gson ;
38
+
39
+ static {
40
+ // Gson 初始化
41
+ GsonBuilder builder = new GsonBuilder ()
42
+ // 序列化为null的字段
43
+ .serializeNulls ()
44
+ // 仅仅处理带有@Expose注解的变量
45
+ .excludeFieldsWithoutExposeAnnotation ()
46
+ // 支持Map
47
+ .enableComplexMapKeySerialization ();
48
+ // 添加对Java8LocalDateTime时间类型的支持
49
+ builder .registerTypeAdapter (LocalDateTime .class , new LocalDateTimeConverter ());
50
+ gson = builder .create ();
51
+ }
52
+
53
+ /**
54
+ * 取得一个全局的Gson
55
+ *
56
+ * @return Gson
57
+ */
58
+ public static Gson getGson () {
59
+ return gson ;
60
+ }
61
+
62
+ public GsonProvider () {
63
+ }
64
+
65
+ @ Override
66
+ public boolean isReadable (Class <?> type , Type genericType ,
67
+ Annotation [] annotations , MediaType mediaType ) {
68
+ return true ;
69
+ }
70
+
71
+ /**
72
+ * 把Json的字符串数据, 转换为T类型的实例
73
+ */
74
+ @ Override
75
+ public T readFrom (Class <T > type , Type genericType , Annotation [] annotations ,
76
+ MediaType mediaType , MultivaluedMap <String , String > httpHeaders ,
77
+ InputStream entityStream ) throws IOException , WebApplicationException {
78
+ try (JsonReader reader = new JsonReader (new InputStreamReader (entityStream , "UTF-8" ))) {
79
+ return gson .fromJson (reader , genericType );
80
+ }
81
+ }
82
+
83
+ @ Override
84
+ public boolean isWriteable (Class <?> type , Type genericType ,
85
+ Annotation [] annotations , MediaType mediaType ) {
86
+ return true ;
87
+ }
88
+
89
+ @ Override
90
+ public long getSize (T t , Class <?> type , Type genericType ,
91
+ Annotation [] annotations , MediaType mediaType ) {
92
+ return -1 ;
93
+ }
94
+
95
+ /**
96
+ * 把一个T类的实例输出到Http输出流中
97
+ */
98
+ @ Override
99
+ public void writeTo (T t , Class <?> type , Type genericType , Annotation [] annotations ,
100
+ MediaType mediaType , MultivaluedMap <String , Object > httpHeaders ,
101
+ OutputStream entityStream ) throws IOException , WebApplicationException {
102
+ //TypeAdapter<T> adapter = gson.getAdapter((TypeToken<T>) TypeToken.get(genericType));
103
+ try (JsonWriter jsonWriter = gson .newJsonWriter (new OutputStreamWriter (entityStream , Charset .forName ("UTF-8" )))) {
104
+ gson .toJson (t , genericType , jsonWriter );
105
+ jsonWriter .close ();
106
+ }
107
+ }
108
+ }
0 commit comments