简体   繁体   English

Spring JSON请求体未映射到Java POJO

[英]Spring JSON request body not mapped to Java POJO

I'm using Spring to implement a RESTful web service. 我正在使用Spring来实现RESTful Web服务。 One of the endpoints takes in a JSON string as request body and I wish to map it to a POJO. 其中一个端点将JSON字符串作为请求体,我希望将其映射到POJO。 However, it seems right now that the passed-in JSON string is not property mapped to the 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. 事实证明,请求体的值没有传入,因为我不仅需要在我的界面中使用@RequestBody注释,而且还需要实际的方法实现。 Once I have that, the problem is solved. 有了这个,问题就解决了。

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. 事实证明,请求体的值没有传入,因为我不仅需要在我的界面中使用@RequestBody注释,而且还需要实际的方法实现。 Once I have that, the problem is solved. 有了这个,问题就解决了。

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"}} NOTE:请求数据为{“customerInfo”:{“firstname”:“xyz”,“lastname”:“XYZ”}}

1st way We can bind above data to the map as below 1st way我们可以将以上数据绑定到地图上,如下所示

@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 2nd way ,我们可以直接绑定它POJO

step 1 create dto class UserInfo.java step 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 class CustomerInfo.java step 1.创建另一个dto类CustomerInfo.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 step 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 谢谢

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。

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

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.
}

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

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