use of org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter 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.xml.MappingJackson2XmlHttpMessageConverter 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.xml.MappingJackson2XmlHttpMessageConverter in project herd by FINRAOS.
the class RestSpringModuleConfig method configureMessageConverters.
/**
* This is called from requestMappingHandlerAdapter to configure the message converters. We override it to configure our own converter in addition to the
* default converters.
*
* @param converters the converter list we configure.
*/
@Override
@SuppressWarnings("rawtypes")
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// Add in our custom converter first.
converters.add(marshallingMessageConverter());
// Add in the default converters (e.g. standard JAXB, Jackson, etc.).
addDefaultHttpMessageConverters(converters);
// Set jackson mapper to include only properties with non-null values.
for (HttpMessageConverter httpMessageConverter : converters) {
if (httpMessageConverter instanceof MappingJackson2XmlHttpMessageConverter) {
converters.remove(httpMessageConverter);
break;
}
}
}
use of org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter 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.xml.MappingJackson2XmlHttpMessageConverter in project spring-framework by spring-projects.
the class WebMvcConfigurationSupport method addDefaultHttpMessageConverters.
/**
* Adds a set of default HttpMessageConverter instances to the given list.
* Subclasses can call this method from {@link #configureMessageConverters}.
* @param messageConverters the list to add the default message converters to
*/
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new ResourceRegionHttpMessageConverter());
if (!shouldIgnoreXml) {
try {
messageConverters.add(new SourceHttpMessageConverter<>());
} catch (Throwable ex) {
// Ignore when no TransformerFactory implementation is available...
}
}
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
if (romePresent) {
messageConverters.add(new AtomFeedHttpMessageConverter());
messageConverters.add(new RssChannelHttpMessageConverter());
}
if (!shouldIgnoreXml) {
if (jackson2XmlPresent) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
} else if (jaxb2Present) {
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
}
if (kotlinSerializationJsonPresent) {
messageConverters.add(new KotlinSerializationJsonHttpMessageConverter());
}
if (jackson2Present) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
} else if (gsonPresent) {
messageConverters.add(new GsonHttpMessageConverter());
} else if (jsonbPresent) {
messageConverters.add(new JsonbHttpMessageConverter());
}
if (jackson2SmilePresent) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build()));
}
if (jackson2CborPresent) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build()));
}
}
Aggregations