1

I am working on a project using angularjs+springboot. Am trying to send email via my application using spring-boot-starter-mail. The message and object of the email are written by the user in a form. what I want to do is to get the message and object values in my RestController using @RequestBody.

the function in my service.js

// send mail
                var sendMail = function(id, objet, msg) {
                    var deferred = $q.defer();
                    $http.post(urlBase + id, objet, msg).then(
                            function(response) {
                                deferred.resolve(response.data);
                            }, function(errResponse) {
                                console.error('Error while sending email');
                                deferred.reject(errResponse);
                            });
                    return deferred.promise;
                }

the method in my restContoller

@RestController public class EmailController {

@Autowired
private JavaMailSender javaMailSender;
@Autowired
UtilisateurService service;

@RequestMapping(value = "/users/{id}", method = RequestMethod.POST)
public ResponseEntity<Void> sendMail(@PathVariable("id") int id, @RequestBody String objet,
        @RequestBody String msg) {
    Utilisateur currentUser = service.findById(id);
    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(currentUser.getEmailUtil());
    message.setSubject(objet);
    message.setText(msg);
    javaMailSender.send(message);
    return new ResponseEntity<Void>(HttpStatus.OK);
}}

This throws this exception :

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<java.lang.Void> com.sla.utilisateur.controller.EmailController.sendMail(int,java.lang.String,java.lang.String)

How can I fix it?

thank you,

2
  • change ResponseEntity<Void> to ResponseEntity<String> Commented Apr 11, 2017 at 13:01
  • Hi, still having the same exception. Commented Apr 11, 2017 at 13:12

1 Answer 1

2

Your usage of $http.post is not correct. You should have a look at the AngularJS POST documentation. $http.post arguments are the following:

post(url, data, [config]);

AngularJS sends the data by default in JSON. So you should send the request using the following statement (for example):

$http.post(urlBase + id, {subject:objet, body:msg})

And in your controller you should define only one @RequestBody maps for the ease of the example to a Map (You could change it to a POJO. ):

@RequestMapping(value = "/users/{id}", method = RequestMethod.POST)
public ResponseEntity<Void> sendMail(@PathVariable("id") int id, @RequestBody Map<String,String> msg) {
    Utilisateur currentUser = service.findById(id);
    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(currentUser.getEmailUtil());
    message.setSubject(msg.get("subject");
    message.setText(msg.get("body"));
    javaMailSender.send(message);
    return new ResponseEntity<Void>(HttpStatus.OK);
}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.