Revize 67aa9c41
Přidáno uživatelem Pavel Fidransky před více než 4 roky(ů)
server/src/main/java/org/danekja/ymanager/RESTConfiguration.java | ||
---|---|---|
1 | 1 |
package org.danekja.ymanager; |
2 | 2 |
|
3 | 3 |
import com.fasterxml.jackson.annotation.JsonInclude; |
4 |
import com.fasterxml.jackson.core.JsonGenerator; |
|
5 |
import com.fasterxml.jackson.core.JsonParser; |
|
6 |
import com.fasterxml.jackson.databind.DeserializationContext; |
|
7 | 4 |
import com.fasterxml.jackson.databind.ObjectMapper; |
8 |
import com.fasterxml.jackson.databind.SerializerProvider; |
|
9 |
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
|
10 | 5 |
import com.fasterxml.jackson.databind.module.SimpleModule; |
11 |
import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
|
6 |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; |
|
7 |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; |
|
8 |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; |
|
9 |
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; |
|
10 |
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; |
|
11 |
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; |
|
12 | 12 |
import org.springframework.beans.factory.annotation.Value; |
13 | 13 |
import org.springframework.context.annotation.Bean; |
14 | 14 |
import org.springframework.context.annotation.Configuration; |
15 | 15 |
import org.springframework.web.servlet.config.annotation.CorsRegistry; |
16 | 16 |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
17 | 17 |
|
18 |
import java.io.IOException; |
|
19 | 18 |
import java.time.LocalDate; |
20 | 19 |
import java.time.LocalDateTime; |
21 | 20 |
import java.time.LocalTime; |
... | ... | |
39 | 38 |
ObjectMapper mapper = new ObjectMapper(); |
40 | 39 |
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
41 | 40 |
|
41 |
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
|
42 |
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm"); |
|
43 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); |
|
44 |
|
|
42 | 45 |
SimpleModule module = new SimpleModule(); |
43 |
module.addDeserializer(LocalDate.class, new LocalDateDeserializer()); |
|
44 |
module.addSerializer(LocalDate.class, new LocalDateSerializer()); |
|
45 |
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer()); |
|
46 |
module.addSerializer(LocalTime.class, new LocalTimeSerializer()); |
|
47 |
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); |
|
48 |
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); |
|
46 |
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
|
|
47 |
module.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
|
|
48 |
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter));
|
|
49 |
module.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter));
|
|
50 |
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
|
|
51 |
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
|
|
49 | 52 |
mapper.registerModule(module); |
50 | 53 |
|
51 | 54 |
return mapper; |
52 | 55 |
} |
53 |
|
|
54 |
class LocalDateDeserializer extends StdDeserializer<LocalDate> { |
|
55 |
|
|
56 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
|
57 |
|
|
58 |
LocalDateDeserializer() { |
|
59 |
super(LocalDate.class); |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
64 |
return LocalDate.parse(jsonParser.readValueAs(String.class), formatter); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
class LocalDateSerializer extends StdSerializer<LocalDate> { |
|
69 |
|
|
70 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
|
71 |
|
|
72 |
LocalDateSerializer() { |
|
73 |
super(LocalDate.class); |
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
77 |
public void serialize(LocalDate value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
78 |
generator.writeString(value.format(formatter)); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
class LocalTimeDeserializer extends StdDeserializer<LocalTime> { |
|
83 |
|
|
84 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); |
|
85 |
|
|
86 |
LocalTimeDeserializer() { |
|
87 |
super(LocalTime.class); |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
92 |
return LocalTime.parse(jsonParser.readValueAs(String.class), formatter); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
class LocalTimeSerializer extends StdSerializer<LocalTime> { |
|
97 |
|
|
98 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); |
|
99 |
|
|
100 |
LocalTimeSerializer() { |
|
101 |
super(LocalTime.class); |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public void serialize(LocalTime value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
106 |
generator.writeString(value.format(formatter)); |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> { |
|
111 |
|
|
112 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); |
|
113 |
|
|
114 |
LocalDateTimeSerializer() { |
|
115 |
super(LocalDateTime.class); |
|
116 |
} |
|
117 |
|
|
118 |
@Override |
|
119 |
public void serialize(LocalDateTime value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
120 |
generator.writeString(value.format(formatter)); |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
class LocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> { |
|
125 |
|
|
126 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); |
|
127 |
|
|
128 |
LocalDateTimeDeserializer() { |
|
129 |
super(LocalDateTime.class); |
|
130 |
} |
|
131 |
|
|
132 |
@Override |
|
133 |
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
134 |
return LocalDateTime.parse(jsonParser.readValueAs(String.class), formatter); |
|
135 |
} |
|
136 |
} |
|
137 | 56 |
} |
Také k dispozici: Unified diff
re #41 replace own (de)serializers of java.time.* classes by Jackson implementation