简体   繁体   中英

I cant test my Spring Boot mail sender API by Postman

I cant test my Spring Boot mail sender API by Postman. I want to create an api with spring boot and send mail. For this, I shared the app properties, service and controller classes below. But when I give the url over postman and add json body to the post method, I get an error. How do I fix this?

Postman Response

{
    "timestamp": "2020-10-29T15:26:23.300+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/mail/send"
}

Postman test params

url: http://localhost:8080/mail/send
method: post
JSON body: 

{
 "to" : "mail@gmail.com",
 "textBody" : "TEST",
 "topic" : "TEST"
}

app properties

spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.gmail.com
spring.mail.username=mail@gmail.com
spring.mail.password=password
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.test-connection=false
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Service Class

@Service
public class SendEmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendEmail(String to, String body, String topic){
        try {
            System.out.println("Mail sending is started");
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom("mutlueren01@gmail.com");
            simpleMailMessage.setTo(to);
            simpleMailMessage.setSubject(topic);
            simpleMailMessage.setText(body);
            javaMailSender.send(simpleMailMessage);
            System.out.println("Mail sending is completed");
        }catch (Exception e){
            System.out.println("Error occured: "+e.getMessage());
        }
    }
}

Controller

@Controller
public class SendEmailController {

    @Autowired
    SendEmailService sendEmailService;

    @RequestMapping(value = "/mail/send/", method = RequestMethod.POST)
    public void sendMail(@RequestParam("to") String to, @RequestParam("textBody") String textBody, @RequestParam("topic") String topic) {
        sendEmailService.sendEmail(to, textBody, topic);
    }

}

The problem is in Controller. You are sending Request body(json), but trying to handle request params. Read about differences.

I'll suggest creating a class:

public class EmailSendingRequest {
String to;
String textBody;
String topic;
//... getters, setters
}

and change your controller method as follows:

    @RequestMapping(value = "/mail/send", method = RequestMethod.POST)
    public void sendMail(@RequestBody EmailSendingRequest emailSendingRequest) {
        sendEmailService.sendEmail(emailSendingRequest.getTo(), 
emailSendingRequest.getTextBody(), emailSendingRequest.getTopic());
    }

PS Or you can send request with request params and don't change anything - query like http://localhost:8080/mail/send?to=mail@gmail.com&textBody=TEST&topic=TEST , but I'm not sure if this is a good idea.

That error code indicates that you want to access resource which does not exist. Try changing the following annotation:

    @RequestMapping(value = "/mail/send/", method = RequestMethod.POST)

to

    @RequestMapping(value = "/mail/send", method = RequestMethod.POST)

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