击中Rest端点时遇到以下异常。如何从String转换为ProtectPanReplyType类?

错误:

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
}

最佳答案

使用ResponseEntity<ProtectPanReplyType>代替ResponseEntity<String>
从您的restOperations.postForEntity()生成并返回ProtectPanReplyType

10-06 03:18