gpt4 book ai didi

java - 管理错误并将其发送给客户端

转载 作者:行者123 更新时间:2023-12-01 09:52:34 24 4
gpt4 key购买 nike

我使用 spring、spring boot 和 spring-rest Controller ,但一开始就有错误。

我的 Controller

@RequestMapping(value = "/members/{memberId}/creditcard", method = RequestMethod.POST)
public void saveCreditCard(@PathVariable("memberId") Long memberId, @RequestBody CreditCardInfoDto creditCardInfo) {
try {
paymentService.saveCreditCard(creditCardInfo);
} catch(Exception e) {}
}

为我服务,

 @Override
public Long saveCreditCard(CreditCardInfoDto creditCardInfo) throws Exception {
Member member = null;

if (creditCardInfo.getMemberId() != null) {
member = memberRepository.findOne(creditCardInfo.getMemberId());
}

List < PaymentGatewayConfig > paymentGateways = paymentGatewayConfigRepository.findAll();

if (paymentGateways != null && !paymentGateways.isEmpty()) {

PaymentGatewayConfig paymentGateway = paymentGateways.get(0);
String paymentGatewayKey = null;

if (member != null) {
paymentGatewayKey = member.getPaymentGatewayKey();
}

ResolveData resdata = null;
ResolverReceipt resreceipt = null;

try {

if (paymentGatewayKey == null) {

ResAddCC resAddCC = new ResAddCC(creditCardInfo.getCreditCard(), creditCardInfo.getDateExpiration(), "7");

//resAddCC.setAvsInfo(avsCheck);
ResolverHttpsPostRequest mpgReq = new ResolverHttpsPostRequest(paymentGateway.getHost(), paymentGateway.getStoreId(), paymentGateway.getApiToken(), resAddCC);
resreceipt = mpgReq.getResolverReceipt();
resdata = resreceipt.getResolveData();

member.setPaymentGatewayKey(resreceipt.getDataKey());
} else {

ResUpdateCC resUpdateCC = new ResUpdateCC(member.getPaymentGatewayKey());
resUpdateCC.setPan(creditCardInfo.getCreditCard());
resUpdateCC.setExpdate(creditCardInfo.getDateExpiration());

ResolverHttpsPostRequest mpgReq = new ResolverHttpsPostRequest(paymentGateway.getHost(), paymentGateway.getStoreId(), paymentGateway.getApiToken(), resUpdateCC);

resreceipt = mpgReq.getResolverReceipt();
resdata = resreceipt.getResolveData();
}
} catch (Exception e) {
log.error("saveCreditCard: " + member.getMemberId() + "ResolverHttpsPostRequest error");
}

if (resdata != null) {
//success
if (Boolean.valueOf(resreceipt.getComplete()) && !Boolean.valueOf(resreceipt.getTimedOut())) {
member.setPaymentGatewayKey(resreceipt.getDataKey());
memberRepository.save(member);
Operation operation = createOperation(resreceipt);
operationRepository.save(operation);
} else {
Operation operation = createOperation(resreceipt);
operationRepository.save(operation);
//payment refused
log.error("saveCreditCard: " + member.getMemberId() + " payment refused error code: " + resreceipt.getResponseCode());
throw new Exception("Payment refused");
}
}
}
return 1 l;
}

因此在服务中,调用 ResolverHttpsPostRequest 时可能会发生错误

如果付款被拒绝,我会抛出异常。

这是管理错误的好方法吗?

如何将这些错误发送到客户端(网络)?

最佳答案

您可以使用org.springframework.http.ResponseEntityjHipster是一个很好的示例项目,可以了解如何实现它。他们使用请求 header 发送一些服务结果,例如成功、失败、警告等。

这是来自 jHipster sample project 的部分代码

@RequestMapping(value = "/operations",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Operation> createOperation(@Valid @RequestBody Operation operation) throws URISyntaxException {
log.debug("REST request to save Operation : {}", operation);
if (operation.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("operation", "idexists", "A new operation cannot already have an ID")).body(null);
}
Operation result = operationRepository.save(operation);
return ResponseEntity.created(new URI("/api/operations/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert("operation", result.getId().toString()))
.body(result);
}

关于java - 管理错误并将其发送给客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37515340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com