Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我在jersy rest post方法中获取一些参数的null值?_Javascript_Java_Json_Angular_Jersey - Fatal编程技术网

Javascript 为什么我在jersy rest post方法中获取一些参数的null值?

Javascript 为什么我在jersy rest post方法中获取一些参数的null值?,javascript,java,json,angular,jersey,Javascript,Java,Json,Angular,Jersey,我正在尝试用jersey设置rest端点。我正在使用angular2客户端 角度代码如下所示: post() { let headers = new Headers({ 'Content-Type': 'application/json' }); this.http.post('http://localhost:8050/rest/addentity', JSON.stringify(this.getValues()), new RequestOptions({ header

我正在尝试用jersey设置rest端点。我正在使用angular2客户端

角度代码如下所示:

  post() {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    this.http.post('http://localhost:8050/rest/addentity', JSON.stringify(this.getValues()), new RequestOptions({ headers: headers })).
    subscribe(data => {
                alert('ok');
          }, error => {
              console.log(JSON.stringify(error.json()));
          });

  }
post发送正确,我可以在有效负载中看到所有值都在那里

但是,对于两个值,我在服务器中总是得到null。方法如下:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("addentity")
public void saveEntity(User user) {
    System.out.println("ADD CALLED");
    System.out.println(user.getId()); // null
    addUser(user);
}
我不知道为什么。媒体类型是正确的,我得到204的回复

有人知道我做错了什么吗

编辑

JSON

使用者


除高度以外的所有值均为
null
0
,因此只有双精度值才能正确读取。

您的实际代码显示了许多问题,我们可以说明:

  • User
    类中,您有一个属性
    userId
    ,而在JSON中,您没有它,而是
    id
  • User
    类中的getter和setter不正确,它们应该具有大写的属性首字母,即:
    getage()
    应该是
    getage()
    setage
    应该是
    setage()
您必须解决这些问题,否则您的POJO将无法正确映射到JSON


你可以参考进一步了解它。

JSON看起来像什么,用户类看起来像什么?@peeskillet我编辑了这篇文章。
{"id":"1234",
"name":"john",
"age":15,
"height":"6.2"}
public class User implements Serializable {

    private static final long serialVersionUID = -5320367665876425279L;

    private Integer userId;
    private String name;
    private double height;
    private int age;

    public User() {
        // TODO Auto-generated constructor stub
    }

    public User(final Integer userId, final String name,
            final double height, final int age) {
        this.userId = userId;
        this.name = name;
        this.height = height;
        this.age = age;
    }

    public Integer getuserId() {
        return userId;
    }

    public void setuserId(final Integer userId) {
        this.userId = userId;
    }

    public String getname() {
        return name;
    }

    public void setname(final String name) {
        this.name = name;
    }

    public double getheight() {
        return height;
    }

    public void setheight(final double height) {
        this.height = height;
    }

    public int getage() {
        return age;
    }

    public void setage(final int age) {
        this.age = age;
    }


}