1

I have a Foo class and a Bar class:

@Entity
public class Foo extends BaseEntity {
    private Long id;
    private String foo;
}

@Entity
public class Bar extends BaseEntity{
    private Long id;
    private String bar;
}

And a @Controller

@Controller
...
    @RequestMapping(value = "/foo/{foo}")
    public void foo(@PathVariable("foo") Foo foo) {
...

With Spring Data JPA and Sprint REST, I'm able to pass e.g. Foo as an id in the URL and automatically get a Foo object via the built-in domain-class-converter http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web.basic.domain-class-converter:

This works fine. But in some cases I want to POST data to the controller via JSON instead of url parameter

{
    foo : {id : 42},
    bar : {id : 123},
}

Is it possible to automatically get that similarly completely loaded from JPA (and not only initialized with the id)?

I tried @RequestBody but that only initializes the values that are given by the request without loading the entity from the db.

6
  • Are you asking about sending JSON data using POST or how to parse given JSON to the object representation? For first check: stackoverflow.com/questions/17964841/… Commented Feb 1, 2017 at 15:40
  • I'm asking about a @Controller receiving JSON Commented Feb 1, 2017 at 15:44
  • so this answer should help: stackoverflow.com/a/36849152/1653268 Commented Feb 1, 2017 at 15:46
  • That answer won't load the JPA entity, but initialize it only with the posted data (the id only). I would like to get the complete object as Jackson does it with @PathVariable Commented Feb 1, 2017 at 15:48
  • I'm afraid this is not possible. Spring Data REST uses GET method for finding data and POST for creating new instances. See 5th chapter of documentation: docs.spring.io/spring-data/rest/docs/current/reference/html Commented Feb 1, 2017 at 16:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.