0

I have some controller method like:

@RequestMapping(value = "/person", method = RequestMethod.POST)
public ResponseEntity<?> create(@RequestBody Person person) {
  personRepository.save(person);
  return new ResponseEntity<>(HttpStatus.OK);
}

The person payload is (the link points to the uri of the department):

{
  "name": "Barack Obama",
  "department": "http://localhost:8080/department/1"
}

I have a PersonResource

public class PersonResource {
  String name;
}

And a 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");
  }
}

DepartmentController

@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);
  }
}

This would result in a json like:

{
  "name": "Barack Obama",
  "_links": {
    "department": {
      "href": "http://localhost:8080/department/1"
    }
  }
}

Now to the problem: When I send the create-Request with the payload, Jackson or Spring Rest/HATEOAS is not able do handle the Links during deserialization. What do I need to configure/implement to make this working?

Thanks!

Benny
  • 1,435
  • 1
  • 15
  • 33

2 Answers2

0

I think here you should return an HttpEntity rather then ResponseEntity

So it would be :

 @RequestMapping(value = "/person", method = RequestMethod.POST)
public HttpEntity create(@RequestBody Person person) {
  personRepository.save(person);
  return new ResponseEntity<>(HttpStatus.OK);
}

Also what DepartmentController looks like ?

Hassam Abdelillah
  • 2,246
  • 3
  • 16
  • 37
  • I added the Department Controller, but this question is not about serialization, but rather about deserialization of the links. ResponseEntity works fine (and by the way is a subclass of HttpEntity). – Benny Apr 15 '16 at 07:23
0

Your posted resource classes must be different from the classes returned by the GET endpoints. They must contain elements to identify the resource to relate, than you have to resolve it by yourself.

If you look at this sample project https://github.com/opencredo/spring-hateoas-sample and specifically to BookController, you may see how a new Book is posted and related Authors are resolved

Nicus
  • 86
  • 7