Arasto
Arasto

Reputation: 491

Unable to read from request body

I'm trying to send an email where the values are dependent on the values from the form in the Frontend, but they are always null.

Controller

    @PostMapping("/send")
    public void sendEmail(Contact contact) throws MessagingException {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo("[email protected]");
        mailMessage.setSubject(contact.getSubject()); //This is always null
        mailMessage.setFrom("[email protected]");
        mailMessage.setText("To confirm ationToken()););");
        emailSenderService.sendEmail(mailMessage);
    }

Entity

public class Contact {

    private String email;
    private String address;
    private String subject;
    private String content;

    public Contact() { }

    //...getters & setters


My Post, This gives me 200 OK enter image description here


Result Subject not being displayed. enter image description here

Any suggestions of where i've gone wrong and how i could correct this are much appreciated.

Upvotes: 0

Views: 110

Answers (1)

Vinay Prajapati
Vinay Prajapati

Reputation: 7504

Prepend method parameter with @RequestBody annotation as below:

public void sendEmail(@RequestBody Contact contact) throws MessagingException {

Upvotes: 1

Related Questions