Revize 42939d08
Přidáno uživatelem Jakub Danek před více než 5 roky(ů)
server/src/main/java/org/danekja/ymanager/RESTConfiguration.java | ||
---|---|---|
1 |
package org.danekja.ymanager; |
|
2 |
|
|
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 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
8 |
import com.fasterxml.jackson.databind.SerializerProvider; |
|
9 |
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
|
10 |
import com.fasterxml.jackson.databind.module.SimpleModule; |
|
11 |
import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
|
12 |
import org.springframework.context.annotation.Bean; |
|
13 |
import org.springframework.context.annotation.Configuration; |
|
14 |
import org.springframework.web.servlet.config.annotation.CorsRegistry; |
|
15 |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
16 |
|
|
17 |
import java.io.IOException; |
|
18 |
import java.time.LocalDate; |
|
19 |
import java.time.LocalDateTime; |
|
20 |
import java.time.LocalTime; |
|
21 |
import java.time.format.DateTimeFormatter; |
|
22 |
|
|
23 |
@Configuration |
|
24 |
public class RESTConfiguration implements WebMvcConfigurer { |
|
25 |
|
|
26 |
@Override |
|
27 |
public void addCorsMappings(CorsRegistry registry) { |
|
28 |
registry.addMapping("/**") |
|
29 |
.allowedMethods("GET", "POST", "PUT", "DELETE"); |
|
30 |
} |
|
31 |
|
|
32 |
@Bean |
|
33 |
public ObjectMapper objectMapper() { |
|
34 |
ObjectMapper mapper = new ObjectMapper(); |
|
35 |
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
|
36 |
|
|
37 |
SimpleModule module = new SimpleModule(); |
|
38 |
module.addDeserializer(LocalDate.class, new LocalDateDeserializer()); |
|
39 |
module.addSerializer(LocalDate.class, new LocalDateSerializer()); |
|
40 |
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer()); |
|
41 |
module.addSerializer(LocalTime.class, new LocalTimeSerializer()); |
|
42 |
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); |
|
43 |
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); |
|
44 |
mapper.registerModule(module); |
|
45 |
|
|
46 |
return mapper; |
|
47 |
} |
|
48 |
|
|
49 |
class LocalDateDeserializer extends StdDeserializer<LocalDate> { |
|
50 |
|
|
51 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
|
52 |
|
|
53 |
LocalDateDeserializer() { |
|
54 |
super(LocalDate.class); |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
59 |
return LocalDate.parse(jsonParser.readValueAs(String.class), formatter); |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
class LocalDateSerializer extends StdSerializer<LocalDate> { |
|
64 |
|
|
65 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
|
66 |
|
|
67 |
LocalDateSerializer() { |
|
68 |
super(LocalDate.class); |
|
69 |
} |
|
70 |
|
|
71 |
@Override |
|
72 |
public void serialize(LocalDate value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
73 |
generator.writeString(value.format(formatter)); |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
class LocalTimeDeserializer extends StdDeserializer<LocalTime> { |
|
78 |
|
|
79 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); |
|
80 |
|
|
81 |
LocalTimeDeserializer() { |
|
82 |
super(LocalTime.class); |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
87 |
return LocalTime.parse(jsonParser.readValueAs(String.class), formatter); |
|
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
class LocalTimeSerializer extends StdSerializer<LocalTime> { |
|
92 |
|
|
93 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); |
|
94 |
|
|
95 |
LocalTimeSerializer() { |
|
96 |
super(LocalTime.class); |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public void serialize(LocalTime value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
101 |
generator.writeString(value.format(formatter)); |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> { |
|
106 |
|
|
107 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); |
|
108 |
|
|
109 |
LocalDateTimeSerializer() { |
|
110 |
super(LocalDateTime.class); |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public void serialize(LocalDateTime value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
115 |
generator.writeString(value.format(formatter)); |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
class LocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> { |
|
120 |
|
|
121 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); |
|
122 |
|
|
123 |
LocalDateTimeDeserializer() { |
|
124 |
super(LocalDateTime.class); |
|
125 |
} |
|
126 |
|
|
127 |
@Override |
|
128 |
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
129 |
return LocalDateTime.parse(jsonParser.readValueAs(String.class), formatter); |
|
130 |
} |
|
131 |
} |
|
132 |
} |
server/src/main/java/org/danekja/ymanager/ws/rest/RESTConfiguration.java | ||
---|---|---|
1 |
package org.danekja.ymanager.ws.rest; |
|
2 |
|
|
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 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
8 |
import com.fasterxml.jackson.databind.SerializerProvider; |
|
9 |
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
|
10 |
import com.fasterxml.jackson.databind.module.SimpleModule; |
|
11 |
import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
|
12 |
import org.springframework.context.annotation.Bean; |
|
13 |
import org.springframework.context.annotation.Configuration; |
|
14 |
import org.springframework.web.servlet.config.annotation.CorsRegistry; |
|
15 |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
16 |
|
|
17 |
import java.io.IOException; |
|
18 |
import java.time.LocalDate; |
|
19 |
import java.time.LocalDateTime; |
|
20 |
import java.time.LocalTime; |
|
21 |
import java.time.format.DateTimeFormatter; |
|
22 |
|
|
23 |
@Configuration |
|
24 |
public class RESTConfiguration implements WebMvcConfigurer { |
|
25 |
|
|
26 |
@Override |
|
27 |
public void addCorsMappings(CorsRegistry registry) { |
|
28 |
registry.addMapping("/**") |
|
29 |
.allowedMethods("GET", "POST", "PUT", "DELETE"); |
|
30 |
} |
|
31 |
|
|
32 |
@Bean |
|
33 |
public ObjectMapper objectMapper() { |
|
34 |
ObjectMapper mapper = new ObjectMapper(); |
|
35 |
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
|
36 |
|
|
37 |
SimpleModule module = new SimpleModule(); |
|
38 |
module.addDeserializer(LocalDate.class, new LocalDateDeserializer()); |
|
39 |
module.addSerializer(LocalDate.class, new LocalDateSerializer()); |
|
40 |
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer()); |
|
41 |
module.addSerializer(LocalTime.class, new LocalTimeSerializer()); |
|
42 |
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); |
|
43 |
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); |
|
44 |
mapper.registerModule(module); |
|
45 |
|
|
46 |
return mapper; |
|
47 |
} |
|
48 |
|
|
49 |
class LocalDateDeserializer extends StdDeserializer<LocalDate> { |
|
50 |
|
|
51 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
|
52 |
|
|
53 |
LocalDateDeserializer() { |
|
54 |
super(LocalDate.class); |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
59 |
return LocalDate.parse(jsonParser.readValueAs(String.class), formatter); |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
class LocalDateSerializer extends StdSerializer<LocalDate> { |
|
64 |
|
|
65 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); |
|
66 |
|
|
67 |
LocalDateSerializer() { |
|
68 |
super(LocalDate.class); |
|
69 |
} |
|
70 |
|
|
71 |
@Override |
|
72 |
public void serialize(LocalDate value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
73 |
generator.writeString(value.format(formatter)); |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
class LocalTimeDeserializer extends StdDeserializer<LocalTime> { |
|
78 |
|
|
79 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); |
|
80 |
|
|
81 |
LocalTimeDeserializer() { |
|
82 |
super(LocalTime.class); |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
87 |
return LocalTime.parse(jsonParser.readValueAs(String.class), formatter); |
|
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
class LocalTimeSerializer extends StdSerializer<LocalTime> { |
|
92 |
|
|
93 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); |
|
94 |
|
|
95 |
LocalTimeSerializer() { |
|
96 |
super(LocalTime.class); |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public void serialize(LocalTime value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
101 |
generator.writeString(value.format(formatter)); |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> { |
|
106 |
|
|
107 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); |
|
108 |
|
|
109 |
LocalDateTimeSerializer() { |
|
110 |
super(LocalDateTime.class); |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public void serialize(LocalDateTime value, JsonGenerator generator, SerializerProvider provider) throws IOException { |
|
115 |
generator.writeString(value.format(formatter)); |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
class LocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> { |
|
120 |
|
|
121 |
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); |
|
122 |
|
|
123 |
LocalDateTimeDeserializer() { |
|
124 |
super(LocalDateTime.class); |
|
125 |
} |
|
126 |
|
|
127 |
@Override |
|
128 |
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
129 |
return LocalDateTime.parse(jsonParser.readValueAs(String.class), formatter); |
|
130 |
} |
|
131 |
} |
|
132 |
} |
server/src/main/java/org/danekja/ymanager/ws/rest/TestController.java | ||
---|---|---|
1 |
package org.danekja.ymanager.ws.rest; |
|
2 |
|
|
3 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
4 |
import org.springframework.web.bind.annotation.RestController; |
|
5 |
|
|
6 |
import static org.springframework.web.bind.annotation.RequestMethod.GET; |
|
7 |
|
|
8 |
@RestController |
|
9 |
public class TestController { |
|
10 |
|
|
11 |
@RequestMapping(value = "/test", method=GET) |
|
12 |
public String hello() { |
|
13 |
return "It works!!"; |
|
14 |
} |
|
15 |
} |
Také k dispozici: Unified diff
re #37 code cleanup