【发布时间】:2016-08-06 12:11:42
【问题描述】:
我有一些控制器方法,例如:
@RequestMapping(value = "/person", method = RequestMethod.POST)
public ResponseEntity<?> create(@RequestBody Person person) {
personRepository.save(person);
return new ResponseEntity<>(HttpStatus.OK);
}
人员有效载荷是(链接指向部门的uri):
{
"name": "Barack Obama",
"department": "http://localhost:8080/department/1"
}
我有一个 PersonResource
public class PersonResource {
String name;
}
还有一个 ResourceAssembler
public class PersonResourceAssembler extends ResourceAssemblerSupport<PersonResource, Person> {
public PersonResourceAssembler() {
super(PersonController.class, PersonResource.class);
}
@Override
public PersonResource toResource(Person person) {
PersonResource res = new PersonResource();
res.setName(person.getName());
res.add(linkTo(methodOn(DepartmentController.class).getOne(person.getDepartment().getId())).withRel("department");
}
}
部门主管
@RestController
@RequestMapping("/department")
public class DepartmentController {
@RequestMapping("/{id}")
public ResponseEntity<DepartmentResource> getOne(@PathVariable("id") Long id) {
Department dep = departmentRepository.findOne(id);
DepartmentResource res = departmentResourceAssembler.toResource(res);
return new ResponseEntity<>(res, HttpStatus.OK);
}
}
这将产生一个像这样的 json:
{
"name": "Barack Obama",
"_links": {
"department": {
"href": "http://localhost:8080/department/1"
}
}
}
现在的问题是:当我发送带有有效负载的 create-Request 时,Jackson 或 Spring Rest/HATEOAS 在反序列化期间无法处理链接。我需要配置/实现什么才能使其正常工作?
谢谢!
【问题讨论】:
-
您自己对此负责。您可以查看 Spring Data REST。
标签: spring-mvc spring-boot jackson spring-hateoas spring-restcontroller