1

我的问题是,如果我调用这个 URL 并得到一个字符串的响应,我得到了它,但是没有 Class ESearchResult。我在这里做错了什么?请帮忙...

 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 类我检查是否有命名空间。

@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;
    
}

来自 URL 的结果

在此处输入图像描述

4

1 回答 1

2

添加以下依赖项:

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

设置标题:

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

这应该工作:

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

ESearchResult esearch = response.getBody();

编辑:考虑RestTemplate将替换为Webclient. 您可以WebClient使用以下内容设置正确的:

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();

并用这个检索你需要的东西:

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

我之前添加的依赖项仍然需要。另外,你也需要这个:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
于 2021-07-01T11:20:07.027 回答