Spring JSON请求体未映射到Java POJO [英] Spring JSON request body not mapped to Java POJO

查看:438
本文介绍了Spring JSON请求体未映射到Java POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这里是@RestController接口

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

数据模型

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

最后我的POST请求:

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

Content-Type指定为application / json



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



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



FIX



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

解决方案

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


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.

here's the @RestController interface

@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:

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

with Content-Type specified to be 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")

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.

解决方案

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.

这篇关于Spring JSON请求体未映射到Java POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆