Skip to content

Commit db29a1a

Browse files
committed
You can now configure the message converters.
This is necessary for modules that needs to customize the date format when sending json responses with jackson.
1 parent d29955c commit db29a1a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

k2-core/src/main/java/com/k2/core/DispatcherServletConfiguration.java

+31
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
.ResourceHandlerRegistry;
99
import org.springframework.web.servlet.config.annotation
1010
.WebMvcConfigurationSupport;
11+
import org.springframework.web.servlet.mvc.method.annotation
12+
.RequestMappingHandlerAdapter;
1113
import org.springframework.web.servlet.mvc.method.annotation
1214
.RequestMappingHandlerMapping;
15+
16+
import java.util.List;
17+
1318
import org.springframework.beans.factory.annotation.Autowired;
1419
import org.springframework.beans.factory.annotation.Value;
1520
import org.springframework.context.annotation.Bean;
1621
import org.springframework.context.annotation.Configuration;
1722
import org.springframework.context.support
1823
.PropertySourcesPlaceholderConfigurer;
24+
import org.springframework.http.converter.HttpMessageConverter;
1925

2026
/** Custom configuration for the dispatcher servlet application context
2127
*
@@ -60,6 +66,31 @@ protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
6066
return result;
6167
}
6268

69+
/** A handler adapter bean that can be configured with the message converters
70+
* found in the application context hierarchy.
71+
*
72+
* If there is at least one message converter up in the application context
73+
* hierarchy, this bean will override the default message converters for the
74+
* handler adapter.
75+
*
76+
* @param converters a list of message converters. If empty, the handler
77+
* adapter uses the default converters (see WebMvcConfigurationSupport).
78+
*
79+
* @return a RequestMappingHandlerAdapter configured with any message
80+
* converter found in the application context. Never returns null.
81+
*/
82+
@Bean
83+
public RequestMappingHandlerAdapter requestMappingHandlerAdapter(
84+
final List<HttpMessageConverter<?>> converters) {
85+
RequestMappingHandlerAdapter adapter;
86+
adapter = super.requestMappingHandlerAdapter();
87+
88+
if (!converters.isEmpty()) {
89+
adapter.setMessageConverters(converters);
90+
}
91+
return adapter;
92+
}
93+
6394
/** Registers handlers that serve the module static content.
6495
*/
6596
@Override

k2-swagger/src/test/java/com/k2/swagger/SwaggerTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
import org.springframework.context.annotation.Bean;
1717
import org.springframework.context.annotation.Configuration;
18+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
19+
import org.springframework.http.converter.json
20+
.MappingJackson2HttpMessageConverter;
1821
import org.springframework.stereotype.Component;
1922

2023
import org.apache.http.client.fluent.Executor;
@@ -23,6 +26,8 @@
2326
import org.apache.http.impl.client.HttpClientBuilder;
2427
import org.apache.http.impl.client.LaxRedirectStrategy;
2528

29+
import com.fasterxml.jackson.databind.ObjectMapper;
30+
import com.fasterxml.jackson.databind.SerializationFeature;
2631
import com.k2.core.Application;
2732
import com.k2.core.ModuleContext;
2833
import com.k2.core.Registrator;
@@ -103,6 +108,13 @@ public void addRegistrations(final ModuleContext moduleContext) {
103108
SwaggerRegistry registry = moduleContext.get(SwaggerRegistry.class);
104109
registry.registerIdl("/module1/static/api1.yaml");
105110
}
111+
112+
@Bean public MappingJackson2HttpMessageConverter
113+
mappingJackson2HttpMessageConverter() {
114+
ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().build();
115+
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
116+
return new MappingJackson2HttpMessageConverter(mapper);
117+
}
106118
}
107119

108120
@Component("module2")

0 commit comments

Comments
 (0)