你好,我有一個問題,我的服務說,"發生了內部服務器錯誤例外!"。例外詳情。IdentificationResponse不能被轉換為org.springframework.http.ResponseEntity""對這個問題有什么幫助?
我的代碼塊如下;
我的IdentificationService Implation
我的IdentificationService Impl;
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)/span> {
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)。
我的handle Response類如下;
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! 例外細節。" response.getBody())。
else if (code == HttpStatus.UNAUTHORIZED)
throw new UnauthorizedException("Unauthorized Exception occured while requesting! 例外細節。" response.getBody())。
else
throw new HttpClientException("Exception occured! 例外細節。" response.getBody(), code.value())。
}
}
下面是我的IdentificationRequest類;
@Data
public class IdentificationRequest {
@Setter(AccessLevel.NONE)
@NotNull[/span
@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;
}
我的Request類如下;
@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;
下面是我的回應類;
@Data
public class IdentificationResponse {
@JsonProperty("IsSuccessful")
private Boolean isSuccessful;
@JsonProperty("代碼")
private Integer code;
@JsonProperty("Message")
private String message;
@JsonProperty("Data")
private Object data;
}
我需要一個地圖來回應物體嗎?
我需要一個地圖來回應物體嗎?
uj5u.com熱心網友回復:
基本上,你正試圖將一個物件投向一個不兼容的物件,如果兩個物件實作了相同的介面,或者物件的層次結構允許,就可以完成投向,如果不能實作這種投向,最好是根據你檢索的物件的值創建一個回應物件的實體。
uj5u.com熱心網友回復:
按照你的代碼,當你呼叫response.getBody()方法時,你會得到IdentificationResponse,并且它正在將其轉換為ResponseEntity,這將拋出ClassCastException。
if (code == HttpStatus.OK || code == HttpStatus.CREATED)
return (T) response.getBody()。
你可以直接回傳response而不是response.getBody()來解決你的問題,或者如果你希望你的createIdentification方法回傳IdentificationResponse實體,你可以按照下面的方法實作
。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()。
你可以找到更多與泛型相關的資訊這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/327327.html
標籤:
