Javascript 为什么我在jersy rest post方法中获取一些参数的null值?
我正在尝试用jersey设置rest端点。我正在使用angular2客户端 角度代码如下所示: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
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
,而在JSON中,您没有它,而是userId
id
- 而
类中的getter和setter不正确,它们应该具有大写的属性首字母,即:User
应该是getage()
,getage()
应该是setage
setage()
你可以参考进一步了解它。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;
}
}