Search in sources :

Example 56 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project goci by EBISPOT.

the class EuropepmcPubMedSearchService method createStudyByPubmed.

public EuropePMCData createStudyByPubmed(String pubmedId) throws PubmedLookupException {
    EuropePMCData europePMCData = new EuropePMCData();
    String urlRequest;
    if (europepmcRoot != null && europepmcSearch != null) {
        urlRequest = europepmcRoot.concat(europepmcSearch);
    } else {
        throw new PubmedLookupException("Unable to search pubmed - no URL configured. " + "Set europepmc properties in your config!");
    }
    String queryUrl = urlRequest.replace("{idlist}", pubmedId);
    ResponseEntity<String> out;
    RestResponseResult result = new RestResponseResult();
    RestTemplate restTemplate = new RestTemplate();
    // restTemplate.setErrorHandler(new CustomResponseErrorHandler());
    // Add the Jackson message converter
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    List<MediaType> mediaTypes = new ArrayList<MediaType>();
    mediaTypes.add(MediaType.TEXT_HTML);
    mediaTypes.add(MediaType.APPLICATION_JSON);
    mediaTypes.add(MediaType.ALL);
    headers.setAccept(mediaTypes);
    // headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON,MediaType.TEXT_HTML));
    HttpEntity<Object> entity = new HttpEntity<Object>(headers);
    getLog().debug("Querying " + queryUrl);
    // and do I need this JSON media type for my use case?
    try {
        out = restTemplate.exchange(queryUrl, HttpMethod.GET, entity, String.class);
        result.setStatus(out.getStatusCode().value());
        result.setUrl(queryUrl);
        System.out.println(queryUrl);
        JsonNode body = new JsonNode(out.getBody().toString());
        result.setRestResult(body);
    } catch (Exception e) {
        throw new PubmedLookupException("EuropePMC : REST API Failed");
    }
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(EuropePMCData.class, new EuropePMCDeserializer());
    mapper.registerModule(module);
    try {
        europePMCData = mapper.readValue(result.getRestResult().toString(), EuropePMCData.class);
    } catch (IOException ioe) {
        System.out.println("EuropePMC : IO Exception - JSON conversion");
        throw new PubmedLookupException("EuropePMC : IO Exception - JSON conversion");
    } catch (Exception e) {
        System.out.println("EuropePMC : Generic Error conversion JSON");
        throw new PubmedLookupException("EuropePMC : Generic Error conversion JSON");
    }
    return europePMCData;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) ArrayList(java.util.ArrayList) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode) IOException(java.io.IOException) EuropePMCData(uk.ac.ebi.spot.goci.utils.EuropePMCData) PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) IOException(java.io.IOException) EuropePMCDeserializer(uk.ac.ebi.spot.goci.utils.EuropePMCDeserializer) RestTemplate(org.springframework.web.client.RestTemplate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 57 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project goci by EBISPOT.

the class DepositionSyncApp method mappingJackson2HttpMessageConverter.

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(getObjectMapper());
    converter.setPrettyPrint(true);
    return converter;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Bean(org.springframework.context.annotation.Bean)

Example 58 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-cloud-microservice-example by kbastani.

the class Application method mappingJackson2HttpMessageConverter.

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    jsonConverter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    objectMapper.registerModule(new Jackson2HalModule());
    jsonConverter.setObjectMapper(objectMapper);
    return jsonConverter;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Jackson2HalModule(org.springframework.hateoas.hal.Jackson2HalModule) Bean(org.springframework.context.annotation.Bean)

Example 59 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-boot-admin by codecentric.

the class AdminServerWebConfiguration method extendMessageConverters.

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    if (!hasConverter(converters, MappingJackson2HttpMessageConverter.class)) {
        ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build();
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
    }
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 60 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project mirrorgate-jira-stories-collector by BBVA.

the class Config method getJiraRestTemplate.

@Bean(JIRA_REST_TEMPLATE)
public RestTemplate getJiraRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(jiraUserName, jiraPassword));
    return restTemplate;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) BasicAuthorizationInterceptor(org.springframework.http.client.support.BasicAuthorizationInterceptor) Bean(org.springframework.context.annotation.Bean)

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