0

在服务器端,我用 Java 和 Jersey/Jackson 开发了一个 REST API,这个 API 调用了 Stripe API。

Stripe API 返回所有具有蛇案例属性名称的对象,例如类 PaymentIntent 的 client_secret。

我的 REST API 使用 Jersey 返回的 JSON 自动将这些属性转换为驼峰式大小写,名称如 clientSecret。

在客户端,我使用 Stripe JS 库,它还需要蛇形大小写的属性名称,因此当我尝试读取 REST API 返回的对象的属性时会出错。我看过很多关于将 Jersey 配置为使用蛇形案例而不是骆驼案例的帖子,但我无法将我发现的内容应用到我的用例中,我自己的课程使用骆驼案例,我的 Stripe 课程使用蛇案例无法控制。

这是我在服务器站点上的当前代码:

package com.knowledgeplaces.metalmsapi.resources;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.knowledgeplaces.metalmsapi.records.PaymentIntentArgsRec;
import com.knowledgeplaces.metalmsapi.records.PaymentIntentResponseRec;
import com.knowledgeplaces.metalmsapi.utils.MetaLmsConstants;
import com.stripe.Stripe;
import com.stripe.model.PaymentIntent;
import com.stripe.exception.StripeException;
import com.stripe.net.ApiResource;

@Path("/eShops/{eShopId}/stripePaymentIntent")  
public class StripePaymentIntentRest {

    // request one Payment Intent
    @POST
    @Produces({ MediaType.APPLICATION_JSON })
    public PaymentIntentResponseRec createService(
            @Context HttpServletRequest req,
            @PathParam("eShopId") Integer eShopId,
            PaymentIntentArgsRec paymentIntentArgs) {
        PaymentIntent paymentIntent;
        PaymentIntentResponseRec paymentIntentResponse;
        // get Stripe API secret key
        if (paymentIntentArgs.stripeTestMode()) {
            Stripe.apiKey = "***********************";
        } else {
            Stripe.apiKey = "***********************";
        }
        // create Payment Intent
        List<Object> paymentMethodTypes = new ArrayList<>();
        paymentMethodTypes.add("card");
        Map<String, Object> params = new HashMap<>();
        params.put("amount", paymentIntentArgs.amount());
        params.put("currency", paymentIntentArgs.currency());
        params.put(
                "payment_method_types",
                paymentMethodTypes);
        try {
            // paymentIntent = PaymentIntent.create(params);
            paymentIntent = ApiResource.GSON.fromJson(PaymentIntent.create(params).toJson(), PaymentIntent.class);
            paymentIntentResponse = new PaymentIntentResponseRec(null, paymentIntent);          
        } catch (StripeException ex) {
            paymentIntentResponse = new PaymentIntentResponseRec(MetaLmsConstants.StripeApiError, null);
        }
        return paymentIntentResponse;
    }

}

使用此代码,我得到了一个带有驼峰式属性的 Payment Intent 对象。如果我取消注释该行

paymentIntent = PaymentIntent.create(params);

并评论该行

paymentIntent = ApiResource.GSON.fromJson(PaymentIntent.create(params).toJson(), PaymentIntent.class);

然后我收到以下错误:

No serializer found for class com.stripe.net.StripeResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.knowledgeplaces.metalmsapi.records.PaymentIntentResponseRec["paymentIntent"]->com.stripe.model.PaymentIntent["lastResponse"])

请告知如何摆脱此错误。

我已经查看了我的代码,这是一个可行的解决方案:

        try {
            paymentIntent = PaymentIntent.create(params);
            String paymentIntentJson = paymentIntent.toJson();
            stringResponse = new StringResponseRec(null, paymentIntentJson);
        } catch (StripeException ex) {
            stringResponse = new StringResponseRec(ex.getMessage(), null);
        }
        return stringResponse;

这个问题与 Jersey Jackson 中的蛇案例支持或 Java 16 中的记录有关,我不知道。

因此,我不是加载具有 Stripe PaymentIntent 对象作为属性的 PaymentIntentResponseRec 类型的记录,而是加载具有字符串类型属性的 StringResponseRec 类型的记录,然后从paymentIntent.toJson()Stripe API 提供的字符串中加载该字符串。

在客户端,我使用 Stripe JS 库的 Angular 应用程序可以很好地获取我的 PaymentIntent 对象。

它有效,但如果您认为有更优雅的解决方案,请随时发表评论。

4

0 回答 0