Search in sources :

Example 11 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter 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);
    }
}
Also used : PathMatchConfigurer(org.springframework.web.servlet.config.annotation.PathMatchConfigurer) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) ResourceHandlerRegistry(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry) EnableSpringDataWebSupport(org.springframework.data.web.config.EnableSpringDataWebSupport) CacheControl(org.springframework.http.CacheControl) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) InterceptorRegistry(org.springframework.web.servlet.config.annotation.InterceptorRegistry) Optional(java.util.Optional) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) NikitaETAGInterceptor(nikita.webapp.web.interceptor.NikitaETAGInterceptor) WebMvcConfigurer(org.springframework.web.servlet.config.annotation.WebMvcConfigurer) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter)

Example 12 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project paascloud-master by paascloud.

the class PcObjectMapper method buidMvcMessageConverter.

public static void buidMvcMessageConverter(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(new JavaTimeModule()).registerModule(simpleModule);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    jackson2HttpMessageConverter.setObjectMapper(objectMapper);
    converters.add(jackson2HttpMessageConverter);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 13 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project alf.io by alfio-event.

the class MvcConfiguration method jacksonMessageConverter.

@Bean
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(objectMapper());
    return converter;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Bean(org.springframework.context.annotation.Bean)

Example 14 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project incubator-servicecomb-java-chassis by apache.

the class ServiceCenterExample method main.

public static void main(String[] args) throws Exception {
    RestTemplate template = new RestTemplate();
    template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("X-Tenant-Name", "default");
    RequestEntity<String> requestEntity = new RequestEntity<String>(headers, HttpMethod.GET, new URI("http://127.0.0.1:9980/registry/v3/microservices"));
    ResponseEntity<String> stringResponseEntity = template.exchange(requestEntity, String.class);
    System.out.println(stringResponseEntity.getBody());
    ResponseEntity<MicroserviceArray> microseriveResponseEntity = template.exchange(requestEntity, MicroserviceArray.class);
    MicroserviceArray microserives = microseriveResponseEntity.getBody();
    System.out.println(microserives.getServices().get(1).getServiceId());
    // instance
    headers.add("X-ConsumerId", microserives.getServices().get(1).getServiceId());
    requestEntity = new RequestEntity<String>(headers, HttpMethod.GET, new URI("http://127.0.0.1:9980/registry/v3/microservices/" + microserives.getServices().get(1).getServiceId() + "/instances"));
    ResponseEntity<String> microserviceInstanceResponseEntity = template.exchange(requestEntity, String.class);
    System.out.println(microserviceInstanceResponseEntity.getBody());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) RequestEntity(org.springframework.http.RequestEntity) URI(java.net.URI)

Example 15 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-cloud-open-service-broker by spring-cloud.

the class NonBindableServiceInstanceBindingControllerIntegrationTest method setUp.

@Before
public void setUp() {
    ServiceInstanceBindingService serviceInstanceBindingService = new NonBindableServiceInstanceBindingService();
    ServiceInstanceBindingController controller = new ServiceInstanceBindingController(catalogService, serviceInstanceBindingService);
    this.mockMvc = MockMvcBuilders.standaloneSetup(controller).setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
}
Also used : NonBindableServiceInstanceBindingService(org.springframework.cloud.servicebroker.service.NonBindableServiceInstanceBindingService) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ServiceInstanceBindingController(org.springframework.cloud.servicebroker.controller.ServiceInstanceBindingController) NonBindableServiceInstanceBindingService(org.springframework.cloud.servicebroker.service.NonBindableServiceInstanceBindingService) ServiceInstanceBindingService(org.springframework.cloud.servicebroker.service.ServiceInstanceBindingService) Before(org.junit.Before)

Aggregations

MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)122 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)51 ArrayList (java.util.ArrayList)46 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)38 Test (org.junit.jupiter.api.Test)28 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)25 RestTemplate (org.springframework.web.client.RestTemplate)25 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)19 HandlerMethod (org.springframework.web.method.HandlerMethod)17 Method (java.lang.reflect.Method)16 Test (org.junit.Test)16 MethodParameter (org.springframework.core.MethodParameter)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bean (org.springframework.context.annotation.Bean)14 List (java.util.List)9 HttpEntity (org.springframework.http.HttpEntity)7 MediaType (org.springframework.http.MediaType)6 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)6