'REST API method doesn't work with json format. [Java]
I am trying to test a REST API method in postman. The idea is that when I test it using x-wwww-form-urlencoded, I get the correct result, but when trying with Json format I get status 500 - "Internal Server Error".
This is the POST method that I am trying to test:
@PostMapping(value = "/send", produces = {"application/json"}, consumes = {"application/x-www-form-urlencoded", "application/json"})
public void sendFeedback(UserDTORegister userDTORegister,
BindingResult bindingResult){
if(bindingResult.hasErrors()){
throw new ValidationException("Feedback is not valid");
}
// create a mail sender
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(this.emailConfig.getHost());
mailSender.setPort(this.emailConfig.getPort());
mailSender.setUsername(this.emailConfig.getUsername());
mailSender.setPassword(this.emailConfig.getPassword());
// create an email instance
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom("[email protected]");
mailMessage.setTo(userDTORegister.getEmail());
mailMessage.setSubject("New account created - Hop Menu");
mailMessage.setText(
"New account created. Welcome to Hop Menu! Credentials ->" +
" email: " + userDTORegister.getEmail() +
" pass: " + userDTORegister.getPassword());
// Send mail
mailSender.send(mailMessage);
}
I would really appreciate if you could help me. Below are the images for my tests, using json or x-www-form-urlencoded.
Solution 1:[1]
Solved it, using @RequestBody before my UserDTORegister param. It seems that you have to use @RequestBody when working with Json format.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mihalache Rares |


