1

I have a spring RepositoryRestController with following method:

@RequestMapping(method = RequestMethod.POST, value = "/doSomethingWithEntity")
    public @ResponseBody ResponseEntity deleteEmployeeSalaryPosition(@RequestBody Resource<Entity> entity)

I want to post an existing entity to this endpoint.

For example Entity class looks like this:

public Entity {
Long id;

String firstField;

String secondField;

EntityB relatedEntity;

}

Posting following JSON to endpoint

{
  id: 1,
  firstField: "someThing",
  secondField: "BUMP",
  relatedEntity: "<root>/api/entityB/1:
}

will result in endpoint deserializing to instace of Entity with following values in its fields

Entity:
  id = null
  firstfield = "someThing"
  secondField = "BUMP",
  relatedEntity = instance of EntityB.class with everything related

What I would expect is:

Entity:
  id = 1
  firstfield = "someThing"
  secondField = "BUMP",
  relatedEntity = instance of EntityB.class with everything related

The question is how to populate id with a value?

I tried all the combinations of _links[self, entity...].

notanormie
  • 435
  • 5
  • 20

1 Answers1

1

In general alot of java-ish frameworks won't bind id passed in JSON. Normally the id is in the path. You want to pass the id then look it up in the repository.

It looks like RepositoryRestController delete is expected to be called with HTTP DELETE with an id in the path: https://docs.spring.io/spring-data/rest/docs/1.0.x/api/org/springframework/data/rest/webmvc/RepositoryRestController.html

But anyhow, for your example you want to put id in the path:

@RequestMapping(method = RequestMethod.POST, value = "/doSomethingWithEntity/{id}")
@ResponseBody
    public ResponseEntity deleteEmployeeSalaryPosition(@PathVariable Long id, @RequestBody Resource<Entity> entity) {

You may not need to pass the request body at all depending on what that method does.

brub
  • 1,083
  • 8
  • 18
  • This is correct. However if we follow the idea that there is no id exposed only self url on the client side. Do you know how to convice spring boot to recover it from relation for me? – notanormie Dec 20 '17 at 16:18
  • this looks related: https://stackoverflow.com/questions/24936636/while-using-spring-data-rest-after-migrating-an-app-to-spring-boot-i-have-obser – brub Dec 20 '17 at 16:21
  • This goes the other way, exposingIds is for sending to client however, what I'm trying to achieve is to deserialize object from JSON with id. – notanormie Dec 21 '17 at 06:42