简体   繁体   中英

Call a REST end-point from another

I have a REST endpoint provided below,

    @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 I work into another end-point where I need to have the reward value from the end-point provided above.

    @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?
    }

How do I get the "reward" in double from the calculateReward method in the end of the 2nd method?

If it's the same application, why not just calling:

nameOfController.calculateReward(userId);

If you can't call the method, you would have to write a client using for example feign .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM