繁体   English   中英

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

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

我进行了映射后调用 - (/login) 我只想在其中调用 (/sendotp) api。 我正在制作一个项目,如果一个人成功登录,他将获得一个 otp。 这是我在 springboot 控制器类中的代码 -

控制器类 ---


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

电子邮件控制器类 -

@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");
    }

如果登录详细信息正确,我只想在 /login 中调用 /send-otp。

您可以像这样创建一个 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);
       }
    }

电子邮件控制器类 -

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

控制器类 ---

@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.

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