【发布时间】:2021-09-16 15:59:37
【问题描述】:
您好,我的服务出现问题,提示“发生内部服务器错误异常!异常详细信息:IdentificationResponse 无法转换为 org.springframework.http.ResponseEntity””对此事有何帮助?
下面是我的代码块;
我的身份识别服务实现;
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);
}
下面是我的句柄响应类;
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 类;
@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;
}
}
下面是我的请求类;
@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("Code")
private Integer code;
@JsonProperty("Message")
private String message;
@JsonProperty("Data")
private Object data;
}
我需要地图来响应响应实体吗?
【问题讨论】:
-
应该是这样的:
return new ResponseEntity<>(your_object, HttpStatus.OK); -
您好,谢谢您的回答,但我已返回句柄响应,我该怎么说?
-
一个很好的方法是使用
@ControllerAdvice。您可以处理所需的所有状态代码的异常。 An example -
你好,谢谢@FaramarzAfzali,但我的逻辑有点不同,所以我需要处理我的通用响应,所以我该怎么做
标签: spring spring-boot rest java-8 controller