The following is a generic "Send an email to a user" endpoint, in a java application with SpringBoot framework.
Hitting it manually with Postman works as intended, but hitting it from the mobile frontend (largely react JS based) with the following JSON:
{recipientAddress: '[email protected]', subjectLine: 'a', messageBody: 's'}
Throws the following error:
Required request body is missing: public boolean
[. . .].Controllers.API.ApplicationUserController.sendEmail([. . .].Models.DTOs.EmailDTO)
I don't know where this would be coming from, as the RequestBody does not have a boolean, and the DTO it does request also does not have a boolean anywhere in it. The only boolean involved is the return value, but that wouldn't be relevant to the exception in question.
CodePudding user response:
Remove @RequestBody. If your DTO will look like this
public class EmailDTO{
private String recipientAddress;
private String subjectLine;
private String messageBody;
//add getters and setters (MANDATORY)
}
and if you send all your params as query params, it will work
if you are indeed need to sent JSON than probably you wanted @PostMapping not @GetMapping
CodePudding user response:
you should not use GET with payload as {recipientAddress: '[email protected]',...}
you can either:
- change
@GetMapping("/SendEmail")to@PostMapping("/SendEmail")and send an object (don't forgetcontent-typeheader)
curl -X POST http://localhost:8080/SendEmail -d '{"recipientAddress": "[email protected]", "subjectLine": "a", "messageBody": "s"}' -H 'Content-Type: application/json'
(on postman make sure to change http method to POST)
- keep
@GetMappingbut break you payload into query params:
curl "http://localhost:8080/[email protected]&subjectLine=a&messageBody=s"
(assuming localhost:8080)
last you did not specify if you use class level annotation @RestController or @Controller, if you use @Controller - you should also add @ResponseBody on method level (use @RestController if you can)

