【问题标题】:spring paypal api context issuespring paypal api上下文问题
【发布时间】:2018-02-13 04:09:44
【问题描述】:

在春季启动应用程序中集成贝宝时,我遇到了这个问题

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.

***************************
APPLICATION FAILED TO START
***************************

Description:

Field apiContext in com.bookstore.service.impl.PaypalService required a bean of type 'com.paypal.base.rest.APIContext' that could not be found.


Action:

Consider defining a bean of type 'com.paypal.base.rest.APIContext' in your configuration.

我的主要看起来像

@SpringBootApplication
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class BookstoreAngularApplication implements CommandLineRunner {

错误指向我使用paypal的api上下文的服务

import com.paypal.api.payments.Transaction;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;
    @Autowired
        private APIContext apiContext;

        public Payment createPayment(
                Double total, 
                String currency, 
                PaypalPaymentMethod method, 
                PaypalPaymentIntent intent, 
                String description, 
                String cancelUrl, 
                String successUrl) throws PayPalRESTException{
            Amount amount = new Amount();
            amount.setCurrency(currency);
            amount.setTotal(total.toString());

            Transaction transaction = new Transaction();
            transaction.setDescription(description);
            transaction.setAmount(amount);

            List<Transaction> transactions = new ArrayList<>();
            transactions.add(transaction);

            Payer payer = new Payer();
            payer.setPaymentMethod(method.toString());

            Payment payment = new Payment();
            payment.setIntent(intent.toString());
            payment.setPayer(payer);
            payment.setTransactions(transactions);
            RedirectUrls redirectUrls = new RedirectUrls();
            redirectUrls.setCancelUrl(cancelUrl);
            redirectUrls.setReturnUrl(successUrl);
            payment.setRedirectUrls(redirectUrls);

            return payment.create(apiContext);
        }

        public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException{
            Payment payment = new Payment();
            payment.setId(paymentId);
            PaymentExecution paymentExecute = new PaymentExecution();
            paymentExecute.setPayerId(payerId);
            return payment.execute(apiContext, paymentExecute);
        }

我有运行 paypal sdk 的项目工作正常,但是当我将它添加到我的应用程序时,我对 paypal api 上下文的这个错误感到恶心

【问题讨论】:

    标签: java spring paypal


    【解决方案1】:

    这是因为我放置配置文件的包的名称必须像 base-packages。* 我在开始时输入了一个快速名称我现在修复它并使其成为它扫描的其他包名称和工作正常

    【讨论】:

      【解决方案2】:

      我以前从未使用过com.paypal.base.rest.APIContext,但您的错误消息表明您需要APIContext 类型的bean,以便可以自动装配。

      考虑定义一个“com.paypal.base.rest.APIContext”类型的bean 你的配置。

      尝试将 bean 添加到您的配置类(或 xml 文件,如果您仍在使用基于 .xml 的配置)。阅读com.paypal.base.rest.APIContextdocumentation 我猜你需要类似的东西:

      @Configuration
      public class AppConfiguration {
      
          @Bean
          public APIContext apiContext() {
              APIContext apiContext = new APIContext("yourClientID", "yourClientSecret", "yourMode");
              return apiContext;
          }
      

      试试这个,看看是否有效。请记住,AppConfiguration 必须放在BookstoreAngularApplication 的子包中,这样才能加载它。这是example

      稍后,从配置文件中读取"yourClientID", "yourClientSecret", "yourMode",而不是对其进行硬编码。

      【讨论】:

      • 我已经像这样配置了它们,并在 Appproperties 中添加了密钥并调用它们检查我楼上的更新请求
      • 但是现在的错误信息是什么?还是一样吗?
      • 他们从一开始就在@Rafa
      • 我猜 spring-boot 没有读取PaypalConfig。确保 spring-boot 加载 PaypalConfig,添加 System.out.println("TetingConfig") 并检查它是否正在加载。另外,请尝试按照以下步骤操作stackoverflow.com/questions/42907553/…
      • 很高兴听到。如果这对您有用,请标记为“已回答”。干杯
      猜你喜欢
      • 2020-12-22
      • 2013-06-16
      • 1970-01-01
      • 2014-01-11
      • 2019-02-11
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 2012-05-31
      相关资源
      最近更新 更多