简体   繁体   English

springboot中如何在另一个api中调用另一个post mapping api

[英]How to call another post mapping api in another api in springboot

I made a postmapping call - (/login) I just want to call (/sendotp) api in it.我进行了映射后调用 - (/login) 我只想在其中调用 (/sendotp) api。 I am making a project where if a person successfully login he will get a otp.我正在制作一个项目,如果一个人成功登录,他将获得一个 otp。 This is my code in controller class in springboot -这是我在 springboot 控制器类中的代码 -

Controller classs ---控制器类 ---


    @PostMapping("/login")
    public SessionHandling loginUser(@RequestBody SessionHandling login) throws Exception {
        String tempEmailId = login.getEmail();
        String takePassword = login.getPassword();
        SessionHandling UserObj = null;
        if(tempEmailId != null && takePassword != null) {
        UserObj = service.fetchUserByEmailIdAndPassword(tempEmailId, takePassword); 
        }
        if(UserObj == null) {
            throw new Exception("invalid / user does not exist");
        }
        return UserObj;
    }
    
    

Emailcontroller class -电子邮件控制器类 -

@PostMapping("/send-otp")
    public String sendOtp(@RequestParam("email") String email) {
        
        int otp = random.nextInt(999999);
        
        String subject = "OTP from session-handling-proj By Harshit";
        String toEmail = email;
        String body = "<h1> OTP = " + otp + "</h1>";
        
          this.emailService.sendMail(toEmail, subject, body);
          return ("succes");
    }

I just want to call /send-otp in /login if the the login details are correct.如果登录详细信息正确,我只想在 /login 中调用 /send-otp。

You could create a ServiceOTP class like this:您可以像这样创建一个 ServiceOTP 类:

public class ServiceOTP {
       public void sendOTP(String email) {
           int otp = random.nextInt(999999);
            
            String subject = "OTP from session-handling-proj By Harshit";
            String toEmail = email;
            String body = "<h1> OTP = " + otp + "</h1>";
            
              this.emailService.sendMail(toEmail, subject, body);
       }
    }

Emailcontroller class -电子邮件控制器类 -

@PostMapping("/send-otp")
public String sendOtp(@RequestParam("email") String email) {
      myServiceOTPInstance.sendOTP(email);
      return ("succes")
}

Controller classs ---控制器类 ---

@PostMapping("/login")
    public SessionHandling loginUser(@RequestBody SessionHandling login) throws Exception {
   //your code
    myServiceOTPInstance.sendOTP(email)
   //your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Springboot2 中将带有 JSON 主体的 POST API 调用转换为 java ZA2F2ED4F8DCEBC2CBB4DC21A? - How to convert a POST API call with a JSON body to a java class in Springboot? 如何在单击按钮时将一些数据从当前 html 页面传递到 controller 作为 springboot thymleaf 中另一个 api 调用的参数? - How to pass some data from current html page to controller on button click as an argument for another api call in springboot thymleaf? 如何在错误 Spring WebFlux 上调用另一个 api - How to call another api on error Spring WebFlux 在API Springboot中映射时遇到问题 - Having trouble with Mapping in API Springboot 如何使用从 apache 骆驼中的另一个 api 获得的参数调用 api? - How call an api with the parameters obtained from another api in apache camel? 一个Rest API调用另一个Rest API - One Rest API that call another Rest API AngularJs POST方法到springboot API - AngularJs POST method to springboot api springboot不映射另一个模块中定义的端点 - springboot not mapping endpoints defined in another module Springboot中调用API的多线程方法 - Multithreaded method to call API in Springboot 如何在 Micronaut 中从我的 controller 调用另一个 rest api - How to call another rest api from my controller in Micronaut
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM