简体   繁体   English

SpringBoot @RequestBody Pojo未映射到我的JSON

[英]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. 我有一个硬编码的json对象,该对象应该映射到我的POJO,但是我在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 json

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

POJO 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. 我会说您必须先创建一个有效的json并测试您的数据是否正确到达了控制器。

Just try to send an example json like that: 只是尝试发送一个示例json像这样:

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

this means you send only a String in a json format. 这意味着您仅发送json格式的字符串。

If you have an Object you maybe have to convert your Object to a json String: 如果您有一个对象,则可能必须将对象转换为json字符串:

var somejson =  JSON.stringify(someobject);

Make properties public and annotate each property with @JsonProperty() (ie @JsonProperty("grouping") ), for example. 例如,公开属性并使用@JsonProperty()(即@JsonProperty(“ grouping”))注释每个属性。 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) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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