Spring JSON请求体未映射到Java POJO

时间:2023-01-12 18:05:34

I'm using Spring to implement a RESTful web service. One of the endpoints takes in a JSON string as request body and I wish to map it to a POJO. However, it seems right now that the passed-in JSON string is not property mapped to the POJO.

我正在使用Spring来实现RESTful Web服务。其中一个端点将JSON字符串作为请求体,我希望将其映射到POJO。但是,现在似乎传入的JSON字符串不是映射到POJO的属性。

here's the @RestController interface

这是@RestController接口

@RequestMapping(value="/send", headers="Accept=application/json", method=RequestMethod.POST)
public void sendEmails(@RequestBody CustomerInfo customerInfo);

the data model

数据模型

public class CustomerInfo {
    private String firstname;
    private String lastname; 
    public CustomerInfo() {
        this.firstname = "first";
        this.lastname = "last";
    }

    public CustomerInfo(String firstname, String lastname)
    {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public String getFirstname(){
        return firstname;
    }

    public void setFirstname(String firstname){
        this.firstname = firstname;
    }

    public String getLastname(){
        return lastname;
    }

    public void getLastname(String lastname){
        this.lastname = lastname;
    }
}

And finally my POST request:

最后我的POST请求:

{"CustomerInfo":{"firstname":"xyz","lastname":"XYZ"}}

with Content-Type specified to be application/json

Content-Type指定为application / json

However, when I print out the object value, the default value("first" and "last") got printed out instead of the value I passed in("xyz" and "XYZ")

但是,当我打印出对象值时,打印出默认值(“first”和“last”)而不是我传入的值(“xyz”和“XYZ”)

Does anyone know why I am not getting the result I expected?

有谁知道为什么我没有得到我预期的结果?

FIX

固定

So it turned out that, the value of request body is not passed in because I need to have the @RequestBody annotation not only in my interface, but in the actual method implementation. Once I have that, the problem is solved.

事实证明,请求体的值没有传入,因为我不仅需要在我的界面中使用@RequestBody注释,而且还需要实际的方法实现。有了这个,问题就解决了。

5 个解决方案

#1


10  

So it turned out that, the value of request body is not passed in because I need to have the @RequestBody annotation not only in my interface, but in the actual method implementation. Once I have that, the problem is solved.

事实证明,请求体的值没有传入,因为我不仅需要在我的界面中使用@RequestBody注释,而且还需要实际的方法实现。有了这个,问题就解决了。

#2


1  

The formatting on this is terrible, but this should work for jackson configuration.

格式化很糟糕,但这应该适用于jackson配置。

<!-- Use Jackson for JSON conversion (POJO to JSON outbound). -->
<bean id="jsonMessageConverter"
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 

<!-- Use JSON conversion for messages -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

ALso, as mentioned in a comment, your JSON is wrong for your object.

另外,正如评论中所提到的,您的JSON对您的对象是错误的。

{"firstname":"xyz",‌​"lastname":"XYZ"}

does appear to be the correct JSON for your object.

看起来确实是您对象的正确JSON。

#3


1  

You can do it in many ways, Here i am going to do it in below different ways-

你可以通过多种方式实现这一目标,我将以不同的方式实现这一目标 -

NOTE: request data shuld be {"customerInfo":{"firstname":"xyz","lastname":"XYZ"}}

注意:请求数据为{“customerInfo”:{“firstname”:“xyz”,“lastname”:“XYZ”}}

1st way We can bind above data to the map as below

第一种方式我们可以将以上数据绑定到地图上,如下所示

@RequestMapping(value = "/send", headers = "Accept=application/json", method = RequestMethod.POST)
public void sendEmails(@RequestBody HashMap<String, HashMap<String, String>> requestData) {

    HashMap<String, String> customerInfo = requestData.get("customerInfo");
    String firstname = customerInfo.get("firstname");
    String lastname = customerInfo.get("lastname");
    //TODO now do whatever you want to do.
}

2nd way we can bind it directly to pojo

我们可以将它直接绑定到pojo的第二种方式

step 1 create dto class UserInfo.java

第1步创建dto类UserInfo.java

public class UserInfo {
    private CustomerInfo customerInfo1;

    public CustomerInfo getCustomerInfo1() {
        return customerInfo1;
    }

    public void setCustomerInfo1(CustomerInfo customerInfo1) {
        this.customerInfo1 = customerInfo1;
    }
}

step 1. create another dto classCustomerInfo.java

步骤1.创建另一个dto classCustomerInfo.java

class CustomerInfo {
        private String firstname;
        private String lastname;

        public String getFirstname() {
            return firstname;
        }

        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }

        public String getLastname() {
            return lastname;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
    }

step 3 bind request body data to pojo

步骤3将请求正文数据绑定到pojo

 @RequestMapping(value = "/send", headers = "Accept=application/json", method = RequestMethod.POST)
    public void sendEmails(@RequestBody UserInfo userInfo) {

        //TODO now do whatever want to do with dto object
    }

I hope it will be help you out. Thanks

我希望它会帮助你。谢谢

#4


0  

Sample Data :

样本数据 :

[  
{  
  "targetObj":{  
     "userId":1,
     "userName":"Devendra"
  }
},
{  
  "targetObj":{  
     "userId":2,
     "userName":"Ibrahim"
  }
},
{  
  "targetObj":{  
     "userId":3,
     "userName":"Suraj"
  }
}
]

For above data this pring controller method working for me:

对于上面的数据,这个pring控制器方法为我工作:

@RequestMapping(value="/saveWorkflowUser", method = RequestMethod.POST)
public void saveWorkflowUser (@RequestBody List<HashMap<String ,HashMap<String , 
  String>>> userList )  {
    System.out.println(" in saveWorkflowUser : "+userList);
 //TODO now do whatever you want to do.
}

#5


-1  

remove those two statements from default constructor and try

从默认构造函数中删除这两个语句并尝试

#1


10  

So it turned out that, the value of request body is not passed in because I need to have the @RequestBody annotation not only in my interface, but in the actual method implementation. Once I have that, the problem is solved.

事实证明,请求体的值没有传入,因为我不仅需要在我的界面中使用@RequestBody注释,而且还需要实际的方法实现。有了这个,问题就解决了。

#2


1  

The formatting on this is terrible, but this should work for jackson configuration.

格式化很糟糕,但这应该适用于jackson配置。

<!-- Use Jackson for JSON conversion (POJO to JSON outbound). -->
<bean id="jsonMessageConverter"
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 

<!-- Use JSON conversion for messages -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

ALso, as mentioned in a comment, your JSON is wrong for your object.

另外,正如评论中所提到的,您的JSON对您的对象是错误的。

{"firstname":"xyz",‌​"lastname":"XYZ"}

does appear to be the correct JSON for your object.

看起来确实是您对象的正确JSON。

#3


1  

You can do it in many ways, Here i am going to do it in below different ways-

你可以通过多种方式实现这一目标,我将以不同的方式实现这一目标 -

NOTE: request data shuld be {"customerInfo":{"firstname":"xyz","lastname":"XYZ"}}

注意:请求数据为{“customerInfo”:{“firstname”:“xyz”,“lastname”:“XYZ”}}

1st way We can bind above data to the map as below

第一种方式我们可以将以上数据绑定到地图上,如下所示

@RequestMapping(value = "/send", headers = "Accept=application/json", method = RequestMethod.POST)
public void sendEmails(@RequestBody HashMap<String, HashMap<String, String>> requestData) {

    HashMap<String, String> customerInfo = requestData.get("customerInfo");
    String firstname = customerInfo.get("firstname");
    String lastname = customerInfo.get("lastname");
    //TODO now do whatever you want to do.
}

2nd way we can bind it directly to pojo

我们可以将它直接绑定到pojo的第二种方式

step 1 create dto class UserInfo.java

第1步创建dto类UserInfo.java

public class UserInfo {
    private CustomerInfo customerInfo1;

    public CustomerInfo getCustomerInfo1() {
        return customerInfo1;
    }

    public void setCustomerInfo1(CustomerInfo customerInfo1) {
        this.customerInfo1 = customerInfo1;
    }
}

step 1. create another dto classCustomerInfo.java

步骤1.创建另一个dto classCustomerInfo.java

class CustomerInfo {
        private String firstname;
        private String lastname;

        public String getFirstname() {
            return firstname;
        }

        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }

        public String getLastname() {
            return lastname;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
    }

step 3 bind request body data to pojo

步骤3将请求正文数据绑定到pojo

 @RequestMapping(value = "/send", headers = "Accept=application/json", method = RequestMethod.POST)
    public void sendEmails(@RequestBody UserInfo userInfo) {

        //TODO now do whatever want to do with dto object
    }

I hope it will be help you out. Thanks

我希望它会帮助你。谢谢

#4


0  

Sample Data :

样本数据 :

[  
{  
  "targetObj":{  
     "userId":1,
     "userName":"Devendra"
  }
},
{  
  "targetObj":{  
     "userId":2,
     "userName":"Ibrahim"
  }
},
{  
  "targetObj":{  
     "userId":3,
     "userName":"Suraj"
  }
}
]

For above data this pring controller method working for me:

对于上面的数据,这个pring控制器方法为我工作:

@RequestMapping(value="/saveWorkflowUser", method = RequestMethod.POST)
public void saveWorkflowUser (@RequestBody List<HashMap<String ,HashMap<String , 
  String>>> userList )  {
    System.out.println(" in saveWorkflowUser : "+userList);
 //TODO now do whatever you want to do.
}

#5


-1  

remove those two statements from default constructor and try

从默认构造函数中删除这两个语句并尝试