I am trying to test my @PostMapping
in on of my @RestController
.
That controller looks like this:
@RestController
public class MyTestController
{
private static final Logger logger = LogManager.getLogger();
@PostMapping(path = "/foo")
public String add(@Valid Boo boo, HttpServletRequest request)
{
if (LoadManager.insert(request.getRemoteAddr()) < 3)
try
{
BooManager.persist(boo);
return "1";
}
catch (Exception e)
{
logger.error(ExceptionUtils.getStackTrace(e));
}
return "0";
}
}
However boo
fields are all null when I make a post request and look at boo object during debug in that add method.
Request body:
{
"officialName" : "test",
"phone" : "test",
"email" : "test",
"churchArea" : "test",
"deanery" : "test",
"town" : "test",
"pass" : "test152S56"
}
Request header Content-Type
is set to application/json
Boo class:
@Data
public class Boo
{
@Size(max = 1000)
@NotBlank
private String officialName;
@Size(max = 100)
@NotBlank
private String phone;
@Size(max = 100)
@NotBlank
private String email;
@Size(max = 100)
@NotBlank
private String churchArea;
@Size(max = 100)
@NotBlank
private String deanery;
@Size(max = 100)
@NotBlank
private String town;
@Size(max = 1000)
@NotBlank
private String pass;
}
@Data annotation is from lombok, which generates, among other things, public getters and setter for that class.
CodePudding user response:
When you need to bound a request body to method parameter you should use @RequestBody annotation. That's why your Boo object attributes are null.
@PostMapping(path = "/foo")
public String add(@RequestBody Boo boo, HttpServletRequest request)