Issue
I am using Spring boot to develop a REST application.
Significant code looks like this:
Controller
@PostMapping(value = "/addUpdateNewsItem")
public ResponseEntity<Object> addUpdateNewsItem(
@RequestBody NimbusNewsDto dto,
Principal principal) {
the DTO:
public class NimbusNewsDto {
private String userDn;
private NimbusNewsJson newsItem;
NimbusNewsJson:
private String nimbusId;
private String subject;
private String description;
private String creator;
private String createDate;
private String expirationDate;
Curl:
curl -k -d @newsItem1.json -H "Content-Type: Application/json" http://localhost:8443/baseline/news/addUpdateNewsItem
the data:
{"newsDto":{
"userDn": "localhost",
"newsItem": {
"nimbusId": "nimbusId1",
"subject": "subject1",
"description": "Hello I am the first news item",
"creator": "God",
"createDate": "23/Jul/2020:02:15:11",
"expirationDate": ""
}
}}
I took a while to get past formatting errors causing 'bad request', etc.. Now when I make the call and debug the code I see that the dto object has the two items, both of which are null. I've tried multiple combinations of " and ' and escaped characters, all with no luck.
I'm running the Ubuntu shell under windows, and the spring code is running on Tomcat.
Suggestions? The last time I ran into this I ended up breaking up the DTO object into multiple parameters, but I rather just have the DTO as input.
Solution
the json you are sending does not "match" the dto, there is an extra object that "wraps" your dto.
you have to remove this part:
{
"newsDto": {
...
}
like this:
{
"userDn": "localhost",
"newsItem": {
"nimbusId": "nimbusId1",
"subject": "subject1",
"description": "Hello I am the first news item",
"creator": "God",
"createDate": "23/Jul/2020:02:15:11",
"expirationDate": ""
}
}
Answered By - MarcoLucidi