Разбор удаленного ответа от запроса RestTemplate - PullRequest
0 голосов
/ 01 июля 2018

Я хочу использовать этот код 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_transaction")
public class PaymentTransaction {
    public enum Response {
        failed_response, successful_response
    }

    @XmlElement(name = "transaction_type")
    public String transactionType;
    @XmlElement(name = "transaction_response")
    public Response transactionResponse;
    @XmlElement(name = "response_code")
    public Integer responseCode;
    @XmlElement(name = "technical_message")
    public String technicalMessage;
    @XmlElement(name = "message")
    ......
}

Полученный объект:

@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

Полный журнал: https://pastebin.com/WdML1yqH

Я всегда получаю Null в ответ. Можете ли вы предложить, как я могу решить эту проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...