Search in sources :

Example 31 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project Gaffer by gchq.

the class RestTemplateProvider method createJsonMessageConverter.

private MappingJackson2HttpMessageConverter createJsonMessageConverter() {
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
    messageConverter.setObjectMapper(new ObjectMapperProvider().getObjectMapper());
    return messageConverter;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ObjectMapperProvider(uk.gov.gchq.gaffer.rest.serialisation.ObjectMapperProvider)

Example 32 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project java-chassis by ServiceComb.

the class SpringMvcIntegrationTestBase method postsEndWithPathParam.

@Test
public void postsEndWithPathParam() throws Exception {
    String result = restTemplate.postForObject(controllerUrl + "sayhello/{name}", null, String.class, "world");
    assertThat(jsonOf(result, String.class), is("hello world"));
    List<HttpMessageConverter<?>> convertersOld = restTemplate.getMessageConverters();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    restTemplate.setMessageConverters(converters);
    result = restTemplate.postForObject(controllerUrl + "sayhello/{name}", null, String.class, "中 国");
    assertThat(result, is("hello 中 国"));
    restTemplate.setMessageConverters(convertersOld);
    ListenableFuture<ResponseEntity<String>> listenableFuture = asyncRestTemplate.postForEntity(controllerUrl + "sayhello/{name}", null, String.class, "world");
    ResponseEntity<String> futureResonse = listenableFuture.get();
    assertThat(jsonOf(futureResonse.getBody(), String.class), is("hello world"));
    asyncRestTemplate.setMessageConverters(converters);
    listenableFuture = asyncRestTemplate.postForEntity(controllerUrl + "sayhello/{name}", null, String.class, "中 国");
    futureResonse = listenableFuture.get();
    assertThat(futureResonse.getBody(), is("hello 中 国"));
    asyncRestTemplate.setMessageConverters(convertersOld);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ResponseEntity(org.springframework.http.ResponseEntity) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 33 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method httpEntityWithContentType.

// SPR-16172
@PathPatternsParameterizedTest
void httpEntityWithContentType(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ResponseEntityController.class, usePathPatterns, wac -> {
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(new MappingJackson2HttpMessageConverter());
        messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
        adapterDef.getPropertyValues().add("messageConverters", messageConverters);
        wac.registerBeanDefinition("handlerAdapter", adapterDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test-entity");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getHeader("Content-Type")).isEqualTo("application/xml");
    assertThat(response.getContentAsString()).isEqualTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<testEntity><name>Foo Bar</name></testEntity>");
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 34 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-framework by spring-projects.

the class ServletInvocableHandlerMethodTests method wrapConcurrentResult_CollectedValuesListWithResponseEntity.

// SPR-15478
@Test
public void wrapConcurrentResult_CollectedValuesListWithResponseEntity() throws Exception {
    List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter());
    ResolvableType elementType = ResolvableType.forClass(Bar.class);
    ReactiveTypeHandler.CollectedValuesList result = new ReactiveTypeHandler.CollectedValuesList(elementType);
    result.add(new Bar("foo"));
    result.add(new Bar("bar"));
    ContentNegotiationManager manager = new ContentNegotiationManager();
    this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters, manager));
    ServletInvocableHandlerMethod hm = getHandlerMethod(new ResponseEntityHandler(), "handleFlux");
    hm = hm.wrapConcurrentResult(result);
    hm.invokeAndHandle(this.webRequest, this.mavContainer);
    assertThat(this.response.getStatus()).isEqualTo(200);
    assertThat(this.response.getContentAsString()).isEqualTo("[{\"value\":\"foo\"},{\"value\":\"bar\"}]");
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.jupiter.api.Test)

Example 35 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-framework by spring-projects.

the class ServletInvocableHandlerMethodTests method wrapConcurrentResult_CollectedValuesList.

@Test
public void wrapConcurrentResult_CollectedValuesList() throws Exception {
    List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter());
    ResolvableType elementType = ResolvableType.forClass(List.class);
    ReactiveTypeHandler.CollectedValuesList result = new ReactiveTypeHandler.CollectedValuesList(elementType);
    result.add(Arrays.asList("foo1", "bar1"));
    result.add(Arrays.asList("foo2", "bar2"));
    ContentNegotiationManager manager = new ContentNegotiationManager();
    this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters, manager));
    ServletInvocableHandlerMethod hm = getHandlerMethod(new MethodLevelResponseBodyHandler(), "handleFluxOfLists");
    hm = hm.wrapConcurrentResult(result);
    hm.invokeAndHandle(this.webRequest, this.mavContainer);
    assertThat(this.response.getStatus()).isEqualTo(200);
    assertThat(this.response.getContentAsString()).isEqualTo("[[\"foo1\",\"bar1\"],[\"foo2\",\"bar2\"]]");
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.jupiter.api.Test)

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