Java 解析来自RestTemplate请求的远程响应

Java 解析来自RestTemplate请求的远程响应,java,jaxb,resttemplate,spring-rest,Java,Jaxb,Resttemplate,Spring Rest,我想使用此RestTemplate代码进行远程POST请求。我试过这个: public static void main(String[] args) { String token = "d778dh"; URI uri = UriComponentsBuilder.fromUriString("http://localhost:8080/bogus_rest_api/v1") .pathSegment(token)

我想使用此RestTemplate代码进行远程POST请求。我试过这个:

       public static void main(String[] args) {
    String token = "d778dh";
    URI uri = UriComponentsBuilder.fromUriString("http://localhost:8080/bogus_rest_api/v1")
            .pathSegment(token)
            .build()
            .toUri();
    HttpEntity<PaymentTransaction> request = new HttpEntity<>(new PaymentTransaction());

    RestTemplate restClient = new RestTemplate();
    restClient.getInterceptors().add(new BasicAuthorizationInterceptor("petrov", "password2"));
    ResponseEntity<PaymentResponse> response = restClient.exchange(uri, HttpMethod.POST,
            request, PaymentResponse.class);
    PaymentResponse foo = response.getBody();
    System.out.println("!!! Calling Bogus Gateway " + foo.getTransactionId());

}
收到的对象:

@XmlRootElement(name = "payment_response")
public class PaymentResponse {

    @XmlElement(name = "transaction_type")
    public String transactionType;
    @XmlElement(name = "transaction_id")
    public String transactionId;
    @XmlElement(name = "amount")
    public Integer amount;
    @XmlElement(name = "currency")
    public String currency;
但得到错误可能是因为我必须配置正确的对象转换

org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@4f1bfe23

23:06:20.309 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:8080/bogus_rest_api/v1/d778dh"
23:06:20.534 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
23:06:20.612 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [org.datalis.plugin.rest.models.PaymentTransaction@747f281] using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@1169afe1]
23:06:20.895 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:8080/bogus_rest_api/v1/d778dh" resulted in 200 (OK)
23:06:20.896 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [class org.datalis.plugin.rest.models.PaymentResponse] as "application/xml;charset=UTF-8" using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@1169afe1]
!!! Calling Bogus Gateway null
完整日志:

我总是得到Null作为响应。
你能建议我如何解决这个问题吗?

请解释问题所在。我这里的每个值都会为Null foo.getTransactionId()。你可能想在问题中实际解释问题。完成,可能是我的请求代码出错了?请尝试编写一个独立的MCVE测试,该测试使用此
MappingJackson2XmlHttpMessageConverter
或其他任何东西解析您得到的XML。另外,尝试将
transactionId
字段更改为
transaction\u id
(同时也是getter/setter)。我觉得不考虑
@xmlement
注释。
org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@4f1bfe23

23:06:20.309 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:8080/bogus_rest_api/v1/d778dh"
23:06:20.534 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
23:06:20.612 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [org.datalis.plugin.rest.models.PaymentTransaction@747f281] using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@1169afe1]
23:06:20.895 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:8080/bogus_rest_api/v1/d778dh" resulted in 200 (OK)
23:06:20.896 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [class org.datalis.plugin.rest.models.PaymentResponse] as "application/xml;charset=UTF-8" using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@1169afe1]
!!! Calling Bogus Gateway null