【发布时间】:2018-01-14 15:49:29
【问题描述】:
在使用 @angular/http method http.put() 更新我的模型时遇到问题。
问题是我根本无法更新位置。我可以成功更新任何其他字段,并且可以在使用 POST 创建时设置任何位置。
我的角度版本是“^4.3.3”
在java中我的模型看起来像
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
String email;
String phone;
Date birthDay;
@JsonBackReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "position_id")
Position position;
}
投影:
public interface EmployeeProjection {
Long getId();
String getName();
String getEmail();
String getPhone();
Date getBirthDay();
@Value("#{target.position.name}")
String getPosition();
}
和位置类:
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
}
在角模板位置:
<md-select mdInput placeholder="Position"
[(ngModel)]="newEmployee.position">
<md-option *ngFor="let position of positions | async" [value]="position.name">{{ position.name }}
</md-option>
</md-select>
我在组件中的更新方法:
update() {
let positionName = this.employee.position;
this.positionService.findByName(positionName).subscribe(position => {
this.employee.position = position._links.self.href;
this.employeeService.update(this.employee);
this.employee.position = positionName;
this.employeeService.localStorageService.set('employee', this.employee);
});
}
在服务中:
update(employee: Employee) {
this.http.put(employee._links.self.href, employee)
.map((resp: Response) => resp.json())
.subscribe(() => {
this.getAll();
});
return employee;
}
在 chrome 中请求:
{
"name": "Nikolai Morgan",
"id": 1,
"position": "http://localhost:9080/api/positions/5",
"birthDay": "1986-07-01",
"email": "NikolaiMorgan@gmail.com",
"phone": "+380840713229",
"_links": {
"self": {
"href": "http://localhost:9080/api/employees/1"
},
"employee": {
"href": "http://localhost:9080/api/employees/1{?projection}",
"templated": true
},
"position": {
"href": "http://localhost:9080/api/employees/1/position"
}
}
}
但响应和预览不包含字段位置:
{
"id" : 1,
"name" : "Nikolai Morgan",
"email" : "NikolaiMorgan@gmail.com",
"phone" : "+380840713229",
"birthDay" : "1986-07-01",
"_links" : {
"self" : {
"href" : "http://localhost:9080/api/employees/1"
},
"employee" : {
"href" : "http://localhost:9080/api/employees/1{?projection}",
"templated" : true
},
"position" : {
"href" : "http://localhost:9080/api/employees/1/position"
}
}
}
【问题讨论】:
-
那么“问题是什么?”,哈姆雷特? ;)
-
如何更新模型中嵌套字段的值?
-
看我的回答...
标签: angular rest spring-data-rest hateoas spring-hateoas