use of uk.ac.ebi.spot.goci.utils.EuropePMCDeserializer 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;
}
Aggregations