简体   繁体   中英

SpringBoot @RequestBody pojo not mapping to my json

Can anyone help me fix this. I have hard coded json object which is suppposed to map to my POJO but I'm getting null values in my Spring Controller. I've checked my getters and setters. They seems to be correct. What am i doing wrong here ?

Controller

@PostMapping("/dashboard")
public Dashboard getDashboard(@RequestBody PaginationRequest paginationRequest) {
      return topcatService.getDashboard(paginationRequest);
}

json

 var paginationRequest = { grouping  : e.target.value ,total : "1", currentPage : "1", pageSize : "5"};

POJO

 public class PaginationRequest {
        private String grouping;
        private String total;
        private String  currentPage;
        private String pageSize;

       //setter/getter
    }

I would say you have to create a valid json first and test if you data comes correctly to your controller.

Just try to send an example json like that:

var paginationRequest = '{\"grouping\":\"anyValue\",\"total\":\"1\",\"currentPage\":\"1\",\"pageSize\": \"5\"}';

this means you send only a String in a json format.

If you have an Object you maybe have to convert your Object to a json String:

var somejson =  JSON.stringify(someobject);

Make properties public and annotate each property with @JsonProperty() (ie @JsonProperty("grouping") ), for example. It's likely your getters and setters don't follow standard naming convention.

I'll try to suggest some small changes,

@RequestMapping(value = "/dashboard", method = RequestMethod.POST, 
                consumes = "application/json", produces = "application/json")
public Dashboard getDashboard(@RequestBody PaginationRequest paginationRequest) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM