如何在Spring Boot中调用Restful WebService

时间:2017-05-10 21:37:47

标签: java spring web-services spring-boot controller

我有一个用于发送电子邮件的SB服务。我想在我现有的应用程序中使用它,我该怎么做?我正在考虑创建一个处理传入HttpRequest和HttpResponse的控制器。但仍然不知道我的现有应用程序将如何调用它。我还需要一些关于SB应用程序如何与其他应用程序独立运行的高级概述。 P.S.-没有用于电子邮件服务的UI界面,因此我不会像在控制器中那样映射URL。

以下是我的示例电子邮件服务:

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

我之前创建了一个这样的控制器来测试它:

   @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有一些商业数据。 我有一个正在使用的jsp,并发布了我正在映射的表单。一切正常。

但是现在我想将它与我现有的应用程序(它的struts 1)集成在一起,所以不会有任何uri来映射。必须从调用应用程序创建某种HttpRequest,我的控制器应该处理它。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您已经实施了此服务吗?然后你需要一个RestController类来映射你选择的uri。在这个类中,您需要注入实现电子邮件发送方法的服务类。这个类是用@Service注释的吗?没有看到你的代码就很难解释。这是REST接口的示例:

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

因此,在这种情况下,emailService将是具有发送电子邮件的方法的类。该类应使用@Service注释。希望有所帮助。

这是您现有的课程:

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

}

如果注入不起作用,您必须使用@ComponentScan({&#34; com.foo.dal。&#34;,&#34; com。)注释您的应用程序类。 foo.notification。&#34;})只需使用服务和资源类的包替换此包。

答案 1 :(得分:0)

我不确定这个问题。如果我是对的,您需要从您的应用程序中调用休息服务。在这种情况下,使用Spring的 /* latin-ext */ @font-face { font-family: 'Great Vibes'; font-style: normal; font-weight: 400; src: local('Great Vibes'), local('GreatVibes-Regular'), url(./asset/fonts/font-sertifikat/sertifikat.woff2) format('woff2'); unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF; } /* latin */ @font-face { font-family: 'Great Vibes'; font-style: normal; font-weight: 400; src: local('Great Vibes'), local('GreatVibes-Regular'), url(./asset/fonts/font-sertifikat/sertifikat4.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215; } p.serif { font-family: 'Great Vibes'; font-size: 70px; padding-top: 250px; } link

会更容易和方便

您可以获得一些概述here