无法从String转换为Spring REST中的类

时间:2016-10-21 19:47:04

标签: java spring rest spring-mvc

在点击Rest端点时获得以下异常。如何从StringProtectPanReplyType类进行类型转换?

错误:

Error - Request: http://localhost:9090/hosted-payments-webapp-1.0.0/pan/protect 
raised java.lang.ClassCastException: com.gsicommerce.api.checkout.ProtectPanReplyType cannot be cast to java.lang.String

ProtectPanServiceImpl.java

@Service
public class ProtectPanServiceImpl implements ProtectPanService {

    @Override
    public ResponseEntity<?> sendProtectPanRequest(ProtectPan protectPan) {
        String pan = protectPan.getPaymentAccountNumber();
        String tenderClass = protectPan.getTenderClass();

        String protectPanRequest = XMLHelper.createProtectPanRequest(pan, tenderClass);
        System.out.println("protectPanRequest = " + protectPanRequest);
        ResponseEntity<String> response = null;
        try {
            response = ApiClientUtils.callClientByEndpointandMessage(protectPanRequest, DEV_PUBLIC_API_URL,
                    ProtectPanReplyType.class);
            System.out.println("response.getClass() = " + response.getClass());

            //DOES NOT WORK
            //ProtectPanReplyType protectPanReplyType = (ProtectPanReplyType)response.getBody();

            //THROWS ClassCastException HERE
            System.out.println(response.getBody());
        } catch (JiBXException e) {
            e.printStackTrace();
        }
        return response;
    }

}

ApiClientUtils.java

public ResponseEntity<String> callClientByEndpointandMessage(String xmlRequest, String endpoint, Class<?> replyType) throws JiBXException {
    HttpEntity<String> request = createRequestForUser("username", "secret",xmlRequest);
    ResponseEntity<String> response = restOperations.postForEntity(endpoint, request, String.class);
    ResponseEntity formattedResponse = new ResponseEntity(null, HttpStatus.BAD_REQUEST);
    try {
        Object jibxObject = JibxHelper.unmarshalMessage(response.getBody(), replyType);
        formattedResponse = new ResponseEntity(jibxObject, HttpStatus.OK);
    } catch (JiBXException e) {
        FaultResponseType faultResponse = JibxHelper.unmarshalMessage(response.getBody(), FaultResponseType.class);
        formattedResponse = new ResponseEntity(faultResponse, HttpStatus.BAD_REQUEST);
    }
    return formattedResponse;
}

ProtectPan.java

public class ProtectPan {

    @JsonProperty("paymentAccountNumber")
    private String paymentAccountNumber;

    @JsonProperty("tenderClass")
    private String tenderClass;

    public String getPaymentAccountNumber() {
        return paymentAccountNumber;
    }

    public String getTenderClass() {
        return tenderClass;
    }
}

ProtectPanReplyType.java

public class ProtectPanReplyType {

    private String token;
    private List<Element> anyList = new ArrayList<Element>();
    private String sessionId;

    //getters and setter removed for brevity
}

2 个答案:

答案 0 :(得分:1)

  1. 使用ResponseEntity<ProtectPanReplyType>代替ResponseEntity<String>
  2. 构建并返回来自restOperations.postForEntity()
  3. 的ProtectPanReplyType

答案 1 :(得分:0)

在做出以下更改后终于能够获得该对象。

<强> ApiClientUtils.java

public ResponseEntity<?> callClientByEndpointandMessage(String xmlRequest, String endpoint, Class<?> replyType) throws JiBXException {
        HttpEntity<String> request = createRequestForUser("payment", "SONitc2m8y", xmlRequest);
        ResponseEntity<String> response = restOperations.postForEntity(endpoint, request, String.class);
        ResponseEntity<?> formattedResponse = null;
        try {
            Object jibxObject = JibxHelper.unmarshalMessage(response.getBody(), replyType);
            formattedResponse = new ResponseEntity(jibxObject, HttpStatus.OK);
        } catch (JiBXException e) {
            FaultResponseType faultResponse = JibxHelper.unmarshalMessage(response.getBody(), FaultResponseType.class);
            formattedResponse = new ResponseEntity(faultResponse, HttpStatus.BAD_REQUEST);
        }
        return formattedResponse;
    }

<强> ProtectPanServiceImpl.java

@Override
public ResponseEntity<?> sendProtectPanRequest(ProtectPan protectPan) {
    String pan = protectPan.getPaymentAccountNumber();
    String tenderClass = protectPan.getTenderClass();

    String protectPanRequest = XMLHelper.createProtectPanRequest(pan, tenderClass);
    ResponseEntity<?> response = null;
    try {
        response = publicApiClientUtils.callClientByEndpointandMessage(protectPanRequest, DEV_PUBLIC_API_URL, ProtectPanReplyType.class);
        ProtectPanReplyType protectPanReplyType = (ProtectPanReplyType) response.getBody();
        System.out.println("protectPanReplyType = " + protectPanReplyType);
    } catch (JiBXException e) {
        e.printStackTrace();
    }
    return response;
}