1

My problem is, if I call this URL and get a response with String, I got it, but with Class ESearchResult no. What I am doing here wrong? Please help...

 String url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=covid";//config.getPmcUrl() + "esearch.fcgi?db=pubmed&term=covid";
    
    
    RestTemplate restTemplate = this.getRestTemplate();
    HttpHeaders headers = this.getHeaders();
    
    HttpEntity<ESearchResult> requestEntity = new HttpEntity<ESearchResult>(headers);
    
    //ResponseEntity<ESearchResult> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, ESearchResult.class);
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
    
    String  esearch = response.getBody(); //this works
    //ESearchResult  esearch = response.getBody();//this doesn't work

ESearchResult class I check with a namespace or without.

@Getter
@Setter
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "eSearchResult", namespace = "http://eutils.ncbi.nlm.nih.gov/eutils/dtd/20060628/esearch.dtd")
public class ESearchResult {
    
    /*@XmlElement(name = "IdList", namespace = "https://eutils.ncbi.nlm.nih.gov/eutils/dtd/20060628/esearch.dtd")
    private List<IdList> idList;*/
    
    @XmlElement(name = "Count", namespace = "https://eutils.ncbi.nlm.nih.gov/eutils/dtd/20060628/esearch.dtd")
    private Integer count;
    
    @XmlElement(name = "RetMax", namespace = "https://eutils.ncbi.nlm.nih.gov/eutils/dtd/20060628/esearch.dtd")
    private Integer retMax;    

    @XmlElement(name = "RetStart", namespace = "https://eutils.ncbi.nlm.nih.gov/eutils/dtd/20060628/esearch.dtd")
    private Integer retStart;
    
}

The result from URL

enter image description here

  • Can you share the error you received from this line `ESearchResult esearch = response.getBody();//this doesn't work` – Occlumency Jul 01 '21 at 11:20

1 Answers1

2

Add the following dependency:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

Set your headers:

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));

and the this should work:

ResponseEntity<ESearchResult> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, ESearchResult.class);

ESearchResult esearch = response.getBody();

EDIT: consider that RestTemplate will be replaced by Webclient. You can setup a proper WebClient with the following:

WebClient wb = WebClient.builder()
            .defaultHeaders(header -> {
                header.setContentType(MediaType.APPLICATION_XML);
            })
            .exchangeStrategies(
                    ExchangeStrategies.builder()
                            .codecs(configurer -> {
                                configurer.defaultCodecs().jaxb2Decoder(new Jaxb2XmlDecoder());
                                configurer.defaultCodecs().jaxb2Encoder(new Jaxb2XmlEncoder());
                            })
                            .build()
            ).build();

And retrieve what you need with this:

ESearchResult  esearch = wb.get().uri("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=covid")
            .retrieve()
            .bodyToMono(ESearchResult.class)
            .block();

The dependency that I added before will be still needed. Also, you need this one too:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Lorelorelore
  • 3,335
  • 8
  • 29
  • 40