Search in sources :

Example 81 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project ma-modules-public by infiniteautomation.

the class BaseFullStackRestTest method setupMvc.

/**
 * @param controllers
 */
protected void setupMvc(Object... controllers) {
    this.objectMapper = MangoRestSpringConfiguration.createNewObjectMapper();
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(this.objectMapper);
    this.mockMvc = standaloneSetup(controllers).setMessageConverters(converter).build();
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)

Example 82 with MappingJackson2HttpMessageConverter

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

the class SpringMvcIntegrationTestBase method ableToPostObjectAsJsonWithRequestVariable.

@Test
public void ableToPostObjectAsJsonWithRequestVariable() {
    Person input = new Person();
    input.setName("world");
    String result = restTemplate.postForObject(controllerUrl + "saysomething?prefix={prefix}", jsonRequest(input), String.class, "hello");
    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);
    input = new Person();
    input.setName("中国");
    result = restTemplate.postForObject(controllerUrl + "saysomething?prefix={prefix}", jsonRequest(input), String.class, "hello");
    assertThat(result, is("hello 中国"));
    restTemplate.setMessageConverters(convertersOld);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) Person(org.apache.servicecomb.demo.compute.Person) Test(org.junit.Test)

Example 83 with MappingJackson2HttpMessageConverter

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

the class SpringMvcIntegrationTestBase method postsEndWithPathParam.

@Test
public void postsEndWithPathParam() {
    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);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 84 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project oncotree by cBioPortal.

the class TopBraidRepository method query.

private List<T> query(String query, ParameterizedTypeReference<List<T>> parameterizedType, boolean refreshSessionOnFailure) throws TopBraidException {
    String sessionId = topBraidSessionConfiguration.getSessionId();
    logger.debug("query() -- sessionId: " + sessionId);
    RestTemplate restTemplate = new RestTemplate();
    // the default supported types for MappingJackson2HttpMessageConverter are:
    // application/json and application/*+json
    // our response content type is application/sparql-results+json-simple
    // NOTE: if the response content type was one of the default types we
    // would not have to add the message converter to the rest template
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
    messageConverter.setSupportedMediaTypes(Collections.singletonList(new MediaType("application", "sparql-results+json-simple")));
    restTemplate.getMessageConverters().add(messageConverter);
    // set our JSESSIONID cookie and our params
    HttpHeaders headers = new HttpHeaders();
    headers.add("Cookie", "JSESSIONID=" + sessionId);
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("format", "json-simple");
    map.add("query", query);
    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
    // See: http://stackoverflow.com/questions/21987295/using-spring-resttemplate-in-generic-method-with-generic-parameter
    try {
        ResponseEntity<List<T>> response = restTemplate.exchange(topBraidURL, HttpMethod.POST, request, parameterizedType);
        logger.debug("query() -- response.getBody(): '" + response.getBody() + "'");
        return response.getBody();
    } catch (RestClientException e) {
        logger.debug("query() -- caught RestClientException");
        // see if we should try again, maybe the session expired
        if (refreshSessionOnFailure == true) {
            // force refresh of the session id
            sessionId = topBraidSessionConfiguration.getFreshSessionId();
            // do not make a second attempt
            return query(query, parameterizedType, false);
        }
        throw new TopBraidException("Failed to connect to TopBraid", e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 85 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project group2-backend by it-kmitl-2018.

the class CustomConfiguration method configureMessageConverters.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new MappingJackson2HttpMessageConverter(Json.jsonMapper));
    converters.add(new MappingJackson2XmlHttpMessageConverter(Xml.xmlMapper));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)

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