如何将响应投射到 responseEntity

how to cast response to responseEntity

您好,我的服务有问题,提示“发生内部服务器错误异常!异常详细信息:无法将 IdentificationResponse 强制转换为 org.springframework.http.ResponseEntity””对此问题有任何帮助吗?

下面是我的代码块;

我的 IdentificationService 实现;

Slf4j
@Service
@RequiredArgsConstructor
public class IdentificationServiceImpl implements IdentificationService {

    private final RestTemplate restTemplate;

    @Value("${platform.url}")
    private String platform;

    private final ServiceConfig serviceConfig;

    @Override
    public ResponseEntity<IdentificationResponse> createIdentification(Request request) {

        IdentificationRequest identificationRequest = new IdentificationRequest(serviceConfig.getIssuerKey(), serviceConfig.getSecureKey());

        identificationRequest.setOperation(ClientConstants.CREATE_IDENTIFICATION_ADD);

        HttpEntity<IdentificationRequest> requestHttpEntity = new HttpEntity<>(IdentificationRequest);

        ResponseEntity<IdentificationResponse> response = restTemplate.exchange(platform, HttpMethod.POST, requestHttpEntity, IdentificationResponse.class);

        return HandleResponse.handleResponse(response);
    }

我的句柄响应如下class;

public static <T> T handleResponse(ResponseEntity response) {
        HttpStatus code = response.getStatusCode();
        if (code == HttpStatus.OK || code == HttpStatus.CREATED) {
            return (T) response.getBody();

        } else if (code == HttpStatus.NO_CONTENT) {
            return null;
        } else if (code == HttpStatus.BAD_REQUEST)
            throw new BadRequestException("BadRequest Exception occured while requesting! Exception details: " + response.getBody());
        else if (code == HttpStatus.UNAUTHORIZED)
            throw new UnauthorizedException("Unauthorized Exception occured while requesting! Exception details: " + response.getBody());
        else
            throw new HttpClientException("Exception occured! Exception details: " + response.getBody(), code.value());
    }
}

我的 IdentificationRequest class 下面;

 @Data
 public class IdentificationRequest {

    @Setter(AccessLevel.NONE)
    @NotNull
    @JsonProperty("IssuerKey")
    private String issuerKey;
    @Setter(AccessLevel.NONE)
    @NotNull
    @JsonProperty("SecurityKey")
    private String secureKey;
    @NotNull
    @JsonProperty("TransactionId")
    private String transactionId;
    @NotNull
    @JsonProperty("TransactionDate")
    @DateTimeFormat(pattern = "YYYY-MM-DD HH:mm:ss.SSS")
    private String transactionDate;
    @NotNull
    @JsonProperty("Msisdn")
    @Pattern(regexp = "^905.{9}", message = "must be of 12 char/digit")
    private Long msisdn;
    private String operation;
    @NotNull
    @JsonProperty("Package")
    private String Package;
    @NotNull
    @JsonProperty("STB")
    private Boolean isStb;
    @JsonProperty("STB_SerialNumber")
    private String stbSerialNumber;
    @JsonProperty("STB_MacAddress")
    private String stbMacAddress;

    public IdentificationRequest(String issuerKey, String secureKey) {
        this.issuerKey = issuerKey;
        this.secureKey = secureKey;
    }
}

我的请求 class 下面;

@Data
public class Request {
    @JsonProperty("OrderId")
    private String orderId;

    @JsonProperty("OrderItemId")
    private String orderItemId;

    @JsonProperty("ProcessInstanceId")
    private String processId;

    @JsonProperty("step_id")
    private String stepId;

    @JsonProperty("step_name")
    private String stepName;

下面是我的回复 class;

@Data
public class IdentificationResponse {

    @JsonProperty("IsSuccessful")
    private Boolean isSuccessful;
    @JsonProperty("Code")
    private Integer code;
    @JsonProperty("Message")
    private String message;
    @JsonProperty("Data")
    private Object data;
}

我需要地图来响应响应实体吗?

基本上你试图将一个对象转换为一个不兼容的对象,如果两个对象都实现相同的接口或者如果对象的层次结构允许,则可以进行转换,如果无法实现此转换,这是一个很好的方法根据您检索到的对象的值创建响应对象实例的想法。

根据您的代码,当您调用 response.getBody() 方法时,您将获得 IdentificationResponse 并且它正在转换它到 ResponseEntity 抛出 ClassCastException.

  if (code == HttpStatus.OK || code == HttpStatus.CREATED)
      return (T) response.getBody();

您可以 return 直接回复 而不是 response.getBody() 来解决您的问题,或者您如果你想让你的 createIdentification 方法成为 return IdentificationResponse instance

return HandleResponse.handleResponse(response, IdentificationResponse.class);

 public static <T> T handleResponse(ResponseEntity response, Class<T> type) {
        HttpStatus code = response.getStatusCode();
        if (code == HttpStatus.OK || code == HttpStatus.CREATED)
            return (T) response.getBody();
 }

您可以找到更多与泛型相关的信息 here