简体   繁体   中英

How to invoke Restful WebService in Spring Boot

I have a SB service that is being used to send email. I wanted to use that in my existing application , how can I do that? I am thinking to create a controller that handles incoming HttpRequest and HttpResponse. But still no idea on how my existing application will invoke it. I need some high level overview too on how exactly SB application will run independently with other application. PS- there is no UI interface for the email service so i wont be mapping url like we do in controllers generally.

Here is my sample email service:

    public class EmailService {
      public HashMap<String, String> sendMessage(String emailFrom, String[] emailToList, String subject, Context ctx) {
     ...../*Business Logic*/
    }
   }

I created a controller like this earlier to test this out:

   @RestController
   public class CourseController {

      @Autowired
      private EmailService emailService;
      @RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
    public void sendEmail() {
    emailService.sendMessage("abc@gmail.com","{client@gmail.com}", "testSubject",new Context);
    }

Context has some business data. I have a jsp that I am using and posting my form through which it is mapping. It all works fine.

But now I want to integrate this with my existing application (its on struts 1)so there wont be any uri to map. There must be some kind of HttpRequest need to be created from the invoking application and my controller should be handling it. How can I achieve this?

You have already this service implemented? Then you need a RestController class that mapps the uri of your choice. In this class you need to inject the service class that realizes your email sending method. Is this class annotated with @Service? Quite difficult to explain without seeing your code. Here an example for a REST-Interface:

@RestController
@RequestMapping("/api/v1/email")
public class RestClass {

 private EmailService emailService;

@Autowired
public RestClass(EmailService emailService){
   this.emailService = emailService;
}
 @RequestMapping(method = RequestMethod.POST)
 public ResponseEntity<?> sendEmail(@RequestBody EmailDTO emailDTO){
          String emailAdress = emailDTO.getEmail();
          this.emailService.sendEmail(emailAdress);
          return new ResponseEntity<>(HttpStatus.NO_CONTENT);
 }
}

So in this case the emailService would be the class that has the method that sends your email. This class should be annotated with @Service. Hope that helps.

Here your existing class:

 @Service
 public class EmailService {
  public HashMap<String, String> sendMessage(String emailFrom,  String[]   emailToList, String subject, Context ctx) {
 ...../*Business Logic*/
}

}

And in case the injection doesn't work you have to annotate your application class with @ComponentScan({"com.foo.dal. ","com.foo.notification. "}) replace this packages simply with the package of your service and resource class.

I am not sure about the problem. If I am right that you need to call a rest service from your application. In this case it is lot easier and convenient to use Spring's RestTemplate link

You can get some overview here

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