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

时间:2016-05-29 21:56:06

标签: java spring spring-boot spring-restcontroller

我使用弹簧,弹簧启动和弹簧控制器,我从错误开始。

我的控制器

@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进行调用时可能会发生错误

如果支付被拒绝,我会提出异常。

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

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

1 个答案:

答案 0 :(得分:1)

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

以下是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);
    }