Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 12 ■ Exposing a REST API Summary In this chapter, you exposed some RESTful calls from your Spring application. You created the controller to expose some of the most common RESTful calls, and you learned how easy it is to use the Spring MVC module to expose these web services. In the next chapter, you will add more services to your Spring application. You will add an e-mail service and schedule some tasks that will help you to check out if the data is accurate. 182

Chapter 13 Adding E-mail and Scheduling Tasks In this chapter, you are going to incorporate new features into your Spring application. Let’s imagine that you have some users that like your My Documents application and they want to subscribe to an e-mail to receive the latest documents that are inserted. Let’s also add some logic that verifies the location of all of the documents (remember that the location is a physical path where the document’s type is a note or PDF and the location for a book type is a URL); if the location is wrong or unavailable, the application will alert you to take some action about it. Let’s start by creating your first e-mail example code. Sending E-mails The Spring Framework provides some libraries for sending e-mails in a very easy way. Listing 13-1 is an EmailService class that just contains a send method. This method will have four parameters: from (the e-mail address where the e-mail is being sent from), to (the recipient’s e-mail), subject (the e-mail’s subject), and the message (the e-mail’s message). Listing 13-1. EmailService.java package com.apress.isf.spring.email; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private MailSender mailSender; public void send(String from, String to, String subject, String message) { sendEmail(from,to,subject,message); } private void sendEmail(String from, String to, String subject, String message){ SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setFrom(from); mailMessage.setTo(to); 183

Chapter 13<br />

Adding E-mail and Scheduling Tasks<br />

In this chapter, you are going to incorporate new features into your <strong>Spring</strong> application. Let’s imagine that you have<br />

some users that like your My Documents application and they want to subscribe to an e-mail to receive the latest<br />

documents that are inserted. Let’s also add some logic that verifies the location of all of the documents (remember<br />

that the location is a physical path where the document’s type is a note or PDF and the location for a book type is a<br />

URL); if the location is wrong or unavailable, the application will alert you to take some action about it.<br />

Let’s start by creating your first e-mail example code.<br />

Sending E-mails<br />

The <strong>Spring</strong> <strong>Framework</strong> provides some libraries for sending e-mails in a very easy way. Listing 13-1 is an EmailService<br />

class that just contains a send method. This method will have four parameters: from (the e-mail address where<br />

the e-mail is being sent from), to (the recipient’s e-mail), subject (the e-mail’s subject), and the message<br />

(the e-mail’s message).<br />

Listing 13-1. EmailService.java<br />

package com.apress.isf.spring.email;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.mail.MailSender;<br />

import org.springframework.mail.SimpleMailMessage;<br />

import org.springframework.stereotype.Service;<br />

@Service<br />

public class EmailService {<br />

@Autowired<br />

private MailSender mailSender;<br />

public void send(String from, String to, String subject, String message) {<br />

sendEmail(from,to,subject,message);<br />

}<br />

private void sendEmail(String from, String to, String subject, String message){<br />

SimpleMailMessage mailMessage = new SimpleMailMessage();<br />

mailMessage.setFrom(from);<br />

mailMessage.setTo(to);<br />

183

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!