Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 129 and 130: Chapter 9 ■ Adding Persistence to
- Page 131 and 132: Chapter 9 ■ Adding Persistence to
- Page 133 and 134: Chapter 10 ■ Showing Your Spring
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
- Page 137 and 138: Chapter 10 ■ Showing Your Spring
- Page 139 and 140: Chapter 10 ■ Showing Your Spring
- Page 141 and 142: Chapter 10 ■ Showing Your Spring
- Page 143 and 144: Chapter 10 ■ Showing Your Spring
- Page 145 and 146: Chapter 11 ■ Integrating Your Spr
- Page 147 and 148: Chapter 11 ■ Integrating Your Spr
- Page 149 and 150: Chapter 11 ■ Integrating Your Spr
- Page 151 and 152: Chapter 11 ■ Integrating Your Spr
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
- Page 159 and 160: Chapter 11 ■ Integrating Your Spr
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
- Page 165 and 166: Chapter 11 ■ Integrating Your Spr
- Page 167 and 168: Chapter 12 Exposing a REST API Nowa
- Page 169 and 170: Chapter 12 ■ Exposing a REST API
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
- Page 177 and 178: Chapter 12 ■ Exposing a REST API
- Page 179: HTTP-DELETE Chapter 12 ■ Exposing
- Page 183 and 184: Chapter 13 ■ Adding E-mail and Sc
- Page 185 and 186: Chapter 13 ■ Adding E-mail and Sc
- Page 187 and 188: Chapter 13 ■ Adding E-mail and Sc
- Page 189 and 190: Chapter 13 ■ Adding E-mail and Sc
- Page 191 and 192: Part III Spring Framework Advanced
- Page 193 and 194: Chapter 14 ■ Using Dynamic Langua
- Page 195 and 196: Chapter 14 ■ Using Dynamic Langua
- Page 197 and 198: Chapter 14 ■ Using Dynamic Langua
- Page 199 and 200: Chapter 14 ■ Using Dynamic Langua
- Page 201 and 202: Chapter 15 ■ Spring Data Within Y
- Page 203 and 204: Chapter 15 ■ Spring Data Within Y
- Page 205 and 206: Chapter 15 ■ Spring Data Within Y
- Page 207 and 208: Chapter 15 ■ Spring Data Within Y
- Page 209 and 210: Chapter 15 ■ Spring Data Within Y
- Page 211 and 212: Chapter 15 ■ Spring Data Within Y
- Page 213 and 214: Chapter 15 ■ Spring Data Within Y
- Page 215 and 216: Chapter 16 ■ Messaging with Your
- Page 217 and 218: Chapter 16 ■ Messaging with Your
- Page 219 and 220: Chapter 16 ■ Messaging with Your
- Page 221 and 222: Chapter 16 ■ Messaging with Your
- Page 223 and 224: Chapter 16 ■ Messaging with Your
- Page 225 and 226: Chapter 16 ■ Messaging with Your
- Page 227 and 228: Chapter 17 Be Social and Go Mobile
- Page 229 and 230: Chapter 17 ■ Be Social and Go Mob
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