从另一个端点调用REST端点

问题描述 投票:1回答:1

我在下面提供了一个REST端点,

    @PutMapping("/calculateReward")
    public ResponseEntity<String> calculateReward(@RequestParam("userId") Long userId) {

        Optional<User> optional = userService.findById(userId);

        if (!optional.isPresent()) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }

        User user = optional.get();

        double reward = user.getCurrentSteps() * Parameters.REWARD_PER_STEPS_EUR;

        RewardList list = new RewardList();

        list.setUser(user);
        list.setReward(reward);

        user.setCurrentSteps(0);
        user.setRewardLists(list);

        userService.save(user);
        rewardListService.save(list);

        JSONObject json = new JSONObject();

        double convertionRateToEuro = currencyMap.get(user.getCurrencyName());
        double rewardConverted = reward * convertionRateToEuro;

        json.put("name", user.getName());
        json.put("currency", user.getCurrencyName());
        json.put("reward", rewardConverted);

        return ResponseEntity.status(HttpStatus.CREATED).body(json.toString());
    }

ATM我工作到另一个终点,我需要从上面提供的终点获得奖励值。

    @PostMapping("/paypal/make/payment")
    public ResponseEntity<String> paymentUsingPaypal(@RequestParam("userId") Long userId) {

        Optional<User> optional = userService.findById(userId);

        if (!optional.isPresent()) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }

        User user = optional.get();


        // How do I get the "reward" in double here?
    }

如何在第二种方法结束时从calculateReward方法获得双倍的“奖励”?

java spring rest spring-boot curl
1个回答
1
投票

如果它是相同的应用程序,为什么不只是调用:

nameOfController.calculateReward(userId);

如果您无法调用该方法,则必须使用feign编写客户端。

© www.soinside.com 2019 - 2024. All rights reserved.