use of org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter in project nikita-noark5-core by HiOA-ABI.
the class AppWebMvcConfiguration method extendMessageConverters.
/**
* Allows us to pretty print JSON/XML output. CLI tools like curl may not prettyprint for you
* so this allows us to format the api output so that any developer testing the system gets nicely
* formatted output.
* However the client should be using Accept: application/json or application/xml when connecting
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
final Optional<HttpMessageConverter<?>> jsonConverterFound = converters.stream().filter(c -> c instanceof MappingJackson2HttpMessageConverter).findFirst();
if (jsonConverterFound.isPresent()) {
final AbstractJackson2HttpMessageConverter converter = (AbstractJackson2HttpMessageConverter) jsonConverterFound.get();
converter.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
// Convert timestamps to readable text strings
converter.getObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
converter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
final Optional<HttpMessageConverter<?>> xmlConverterFound = converters.stream().filter(c -> c instanceof MappingJackson2XmlHttpMessageConverter).findFirst();
if (jsonConverterFound.isPresent()) {
final MappingJackson2XmlHttpMessageConverter converter = (MappingJackson2XmlHttpMessageConverter) xmlConverterFound.get();
converter.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
converter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
}
use of org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter in project nikita-noark5-core by HiOA-ABI.
the class AppWebMvcConfiguration method extendMessageConverters.
/**
* Allows us to pretty print JSON/XML output. CLI tools like curl may not prettyprint for you
* so this allows us to format the api output so that any developer testing the system gets nicely
* formatted output.
* However the client should be using Accept: application/json or application/xml when connecting
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
final Optional<HttpMessageConverter<?>> jsonConverterFound = converters.stream().filter(c -> c instanceof MappingJackson2HttpMessageConverter).findFirst();
if (jsonConverterFound.isPresent()) {
final AbstractJackson2HttpMessageConverter converter = (AbstractJackson2HttpMessageConverter) jsonConverterFound.get();
converter.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
// Convert timestamps to readable text strings
converter.getObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
converter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
final Optional<HttpMessageConverter<?>> xmlConverterFound = converters.stream().filter(c -> c instanceof MappingJackson2XmlHttpMessageConverter).findFirst();
if (jsonConverterFound.isPresent()) {
final MappingJackson2XmlHttpMessageConverter converter = (MappingJackson2XmlHttpMessageConverter) xmlConverterFound.get();
converter.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
converter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
}
use of org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportTests method requestMappingHandlerAdapter.
@Test
public void requestMappingHandlerAdapter() {
ApplicationContext context = initContext(WebConfig.class);
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
assertThat(converters.size()).isEqualTo(12);
converters.stream().filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter).forEach(converter -> {
ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
assertThat(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
assertThat(mapper.getClass()).isEqualTo(XmlMapper.class);
}
});
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
assertThat(initializer).isNotNull();
ConversionService conversionService = initializer.getConversionService();
assertThat(conversionService).isNotNull();
boolean condition1 = conversionService instanceof FormattingConversionService;
assertThat(condition1).isTrue();
Validator validator = initializer.getValidator();
assertThat(validator).isNotNull();
boolean condition = validator instanceof LocalValidatorFactoryBean;
assertThat(condition).isTrue();
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
@SuppressWarnings("unchecked") List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
assertThat(bodyAdvice.size()).isEqualTo(2);
assertThat(bodyAdvice.get(0).getClass()).isEqualTo(JsonViewRequestBodyAdvice.class);
assertThat(bodyAdvice.get(1).getClass()).isEqualTo(JsonViewResponseBodyAdvice.class);
}
use of org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter in project spring-framework by spring-projects.
the class MvcNamespaceTests method testDefaultConfig.
@Test
public void testDefaultConfig() throws Exception {
loadBeanDefinitions("mvc-config.xml");
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
assertThat(mapping).isNotNull();
assertThat(mapping.getOrder()).isEqualTo(0);
assertThat(mapping.getUrlPathHelper().shouldRemoveSemicolonContent()).isTrue();
mapping.setDefaultHandler(handlerMethod);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
NativeWebRequest webRequest = new ServletWebRequest(request);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
assertThat(manager.resolveMediaTypes(webRequest)).as("Should not resolve file extensions by default").containsExactly(MediaType.ALL);
RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
assertThat(adapter).isNotNull();
assertThat(new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")).asInstanceOf(BOOLEAN).isFalse();
List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
assertThat(converters.size() > 0).isTrue();
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof AbstractJackson2HttpMessageConverter) {
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
assertThat(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
assertThat(objectMapper.getClass()).isEqualTo(XmlMapper.class);
}
}
}
assertThat(appContext.getBean(FormattingConversionServiceFactoryBean.class)).isNotNull();
assertThat(appContext.getBean(ConversionService.class)).isNotNull();
assertThat(appContext.getBean(LocalValidatorFactoryBean.class)).isNotNull();
assertThat(appContext.getBean(Validator.class)).isNotNull();
assertThat(appContext.getBean("localeResolver", LocaleResolver.class)).isNotNull();
assertThat(appContext.getBean("themeResolver", ThemeResolver.class)).isNotNull();
assertThat(appContext.getBean("viewNameTranslator", RequestToViewNameTranslator.class)).isNotNull();
assertThat(appContext.getBean("flashMapManager", FlashMapManager.class)).isNotNull();
// default web binding initializer behavior test
request = new MockHttpServletRequest("GET", "/");
request.addParameter("date", "2009-10-31");
request.addParameter("percent", "99.99%");
MockHttpServletResponse response = new MockHttpServletResponse();
HandlerExecutionChain chain = mapping.getHandler(request);
assertThat(chain.getInterceptorList().size()).isEqualTo(1);
assertThat(chain.getInterceptorList().get(0) instanceof ConversionServiceExposingInterceptor).isTrue();
ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptorList().get(0);
interceptor.preHandle(request, response, handlerMethod);
assertThat(request.getAttribute(ConversionService.class.getName())).isSameAs(appContext.getBean(ConversionService.class));
adapter.handle(request, response, handlerMethod);
assertThat(handler.recordedValidationError).isTrue();
assertThat(handler.date).isInSameDayAs("2009-10-31T00:00:00+00:00");
assertThat(handler.percent).isEqualTo(Double.valueOf(0.9999));
CompositeUriComponentsContributor uriComponentsContributor = this.appContext.getBean(MvcUriComponentsBuilder.MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME, CompositeUriComponentsContributor.class);
assertThat(uriComponentsContributor).isNotNull();
String name = "mvcHandlerMappingIntrospector";
HandlerMappingIntrospector introspector = this.appContext.getBean(name, HandlerMappingIntrospector.class);
assertThat(introspector).isNotNull();
assertThat(introspector.getHandlerMappings().size()).isEqualTo(2);
assertThat(introspector.getHandlerMappings().get(0)).isSameAs(mapping);
assertThat(introspector.getHandlerMappings().get(1).getClass()).isEqualTo(BeanNameUrlHandlerMapping.class);
}
Aggregations