|
18 | 18 | */ |
19 | 19 | package de.rwth.idsg.steve.config; |
20 | 20 |
|
21 | | -import com.fasterxml.jackson.databind.DeserializationFeature; |
22 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
23 | | -import com.fasterxml.jackson.databind.SerializationFeature; |
| 21 | +import com.fasterxml.jackson.datatype.joda.JodaModule; |
24 | 22 | import com.mysql.cj.conf.PropertyKey; |
25 | 23 | import com.zaxxer.hikari.HikariConfig; |
26 | 24 | import com.zaxxer.hikari.HikariDataSource; |
|
35 | 33 | import org.jooq.impl.DSL; |
36 | 34 | import org.jooq.impl.DataSourceConnectionProvider; |
37 | 35 | import org.jooq.impl.DefaultConfiguration; |
| 36 | +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; |
38 | 37 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
39 | 38 | import org.springframework.context.annotation.Bean; |
40 | 39 | import org.springframework.context.annotation.ComponentScan; |
41 | 40 | import org.springframework.context.annotation.Configuration; |
42 | 41 | import org.springframework.context.annotation.Primary; |
43 | | -import org.springframework.format.support.FormattingConversionService; |
44 | | -import org.springframework.http.converter.HttpMessageConverter; |
45 | | -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
46 | 42 | import org.springframework.scheduling.annotation.EnableAsync; |
47 | 43 | import org.springframework.scheduling.annotation.EnableScheduling; |
48 | 44 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
49 | 45 | import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; |
50 | | -import org.springframework.web.accept.ContentNegotiationManager; |
51 | | -import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
52 | 46 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
53 | | -import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; |
54 | 47 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
55 | | -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; |
56 | 48 | import org.springframework.web.servlet.view.InternalResourceViewResolver; |
57 | 49 |
|
58 | 50 | import javax.sql.DataSource; |
59 | | -import java.util.List; |
| 51 | + |
| 52 | +import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; |
| 53 | +import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS; |
60 | 54 |
|
61 | 55 | /** |
62 | 56 | * Configuration and beans of Spring Framework. |
|
66 | 60 | */ |
67 | 61 | @Slf4j |
68 | 62 | @Configuration |
69 | | -@EnableWebMvc |
70 | 63 | @EnableScheduling |
71 | 64 | @EnableAsync |
72 | 65 | @ComponentScan("de.rwth.idsg.steve") |
@@ -200,34 +193,15 @@ public void addResourceHandlers(final ResourceHandlerRegistry registry) { |
200 | 193 | // API config |
201 | 194 | // ------------------------------------------------------------------------- |
202 | 195 |
|
203 | | - @Override |
204 | | - public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { |
205 | | - for (HttpMessageConverter<?> converter : converters) { |
206 | | - if (converter instanceof MappingJackson2HttpMessageConverter conv) { |
207 | | - ObjectMapper objectMapper = conv.getObjectMapper(); |
208 | | - objectMapper.findAndRegisterModules(); |
209 | | - // if the client sends unknown props, just ignore them instead of failing |
210 | | - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
211 | | - // default is true |
212 | | - objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
213 | | - break; |
214 | | - } |
215 | | - } |
216 | | - } |
217 | | - |
218 | | - /** |
219 | | - * Find the ObjectMapper used in MappingJackson2HttpMessageConverter and initialized by Spring automatically. |
220 | | - * MappingJackson2HttpMessageConverter is not a Bean. It is created in {@link WebMvcConfigurationSupport#addDefaultHttpMessageConverters(List)}. |
221 | | - * Therefore, we have to access it via proxies that reference it. RequestMappingHandlerAdapter is a Bean, created in |
222 | | - * {@link WebMvcConfigurationSupport#requestMappingHandlerAdapter(ContentNegotiationManager, FormattingConversionService, org.springframework.validation.Validator)}. |
223 | | - */ |
224 | 196 | @Bean |
225 | | - @Primary |
226 | | - public ObjectMapper jacksonObjectMapper(RequestMappingHandlerAdapter requestMappingHandlerAdapter) { |
227 | | - return requestMappingHandlerAdapter.getMessageConverters().stream() |
228 | | - .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter) |
229 | | - .findAny() |
230 | | - .map(conv -> ((MappingJackson2HttpMessageConverter) conv).getObjectMapper()) |
231 | | - .orElseThrow(() -> new RuntimeException("There is no MappingJackson2HttpMessageConverter in Spring context")); |
| 197 | + public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() { |
| 198 | + return builder -> { |
| 199 | + // default is true |
| 200 | + builder.featuresToDisable(WRITE_DATES_AS_TIMESTAMPS); |
| 201 | + // if the client sends unknown props, just ignore them instead of failing |
| 202 | + builder.featuresToDisable(FAIL_ON_UNKNOWN_PROPERTIES); |
| 203 | + // we still use joda DateTime... |
| 204 | + builder.modulesToInstall(new JodaModule()); |
| 205 | + }; |
232 | 206 | } |
233 | 207 | } |
0 commit comments