spring引导将来自angular2的JSON post请求中的空属性映射到POJO
在对这个话题做了大量研究之后,我决定在这里提问。我将所有空属性都传递给POJO/Model,它应该从我从Angular 2前端发布的JSON中获取值。以下是rest控制器方法:spring引导将来自angular2的JSON post请求中的空属性映射到POJO,json,spring,rest,angular,spring-boot,Json,Spring,Rest,Angular,Spring Boot,在对这个话题做了大量研究之后,我决定在这里提问。我将所有空属性都传递给POJO/Model,它应该从我从Angular 2前端发布的JSON中获取值。以下是rest控制器方法: @RequestMapping(value = "/employees/update", method = RequestMethod.POST, consumes = "application/json") public String allEmployees( @RequestBody Employee e
@RequestMapping(value = "/employees/update", method = RequestMethod.POST, consumes = "application/json")
public String allEmployees( @RequestBody Employee emp){
return "";
}
以下是POJO/Model/Hibernate实体:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, updatable = false)
private Long id;
private String firstname;
private String lastname;
private String department;
public Employee(){}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
以下是角度2维修方法:
updateEmployee(emp:Employee){
let url: string = "http://localhost:8080/api/employees/update";
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post(url, {emp}, {headers: headers, withCredentials: true }).map(res => res.json());
}
以及Angular 2的员工界面:
export interface Employee{
id: number;
firstname: string;
lastname: string;
department: string;
}
我做错了什么?我搜索过类似的问题,但没有一个适用于我的案例。谢谢大家! 尝试用 这将成为:
@ResponseBody
@RequestMapping(value = "/employees/update", method = RequestMethod.POST, consumes = "application/json")
public String allEmployees( @RequestBody Employee emp){
return "";
}
请尝试使用注释方法 这将成为:
@ResponseBody
@RequestMapping(value = "/employees/update", method = RequestMethod.POST, consumes = "application/json")
public String allEmployees( @RequestBody Employee emp){
return "";
}
在发送javascript对象之前,需要序列化它。尝试:
this.http.post(url, JSON.stringify(emp), {headers: headers, withCredentials: true }).map(res => res.json());
在发送javascript对象之前,需要序列化它。尝试:
this.http.post(url, JSON.stringify(emp), {headers: headers, withCredentials: true }).map(res => res.json());
试着用@ResponseBodyit注释这个方法,它仍然是一样的…所有的属性都是空的,我刚刚解决了它,实际上它没有映射,因为我在emp变量周围放了一个花括号。非常感谢您的帮助尝试用@ResponseBodyit注释该方法仍然是一样的…所有的属性都是null我刚刚解决了它,实际上它没有映射,因为我在emp变量周围放了花括号。非常感谢您的帮助谢谢,但我已经尝试过了,没有什么不同…我相信问题出在后端而不是前端。那么,有效负载发送是否确实正确?检查浏览器控制台上的网络选项卡。我刚刚解决了它,实际上它没有映射,因为我在emp变量周围放了花括号。非常感谢您的帮助谢谢,但我已经尝试过了,没有什么不同…我相信问题出在后端而不是前端。那么,有效负载发送是否确实正确?检查浏览器控制台上的网络选项卡。我刚刚解决了它,实际上它没有映射,因为我在emp变量周围放了花括号。非常感谢您的帮助关于emp参数仍然是一样的。它的所有属性都设置为null。关于参数emp,它仍然是相同的。其所有属性都设置为空。