1

The RepositoryRestResource annotation in Spring creates endpoints on a defined path fragment, e.g.

@RepositoryRestResource(path = "expenses")

would create endpoints like:

/expenses
/expenses/search
/expenses/1

Is there a way to include a path parameter in that repository? I want to see expenses only related to a specific user, e.g. "michael":

/users/michael/expenses
/users/michael/expenses/search
/users/michael/expenses/1

EDIT 1

I managed to get halfway there by using the same annotation on the User, which results in the right endpoint:

/users/1/expenses

BUT I cannot POST anything on that resource - the server answers with empty body.

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
  • Possible duplicate of [POSTing a @OneToMany sub-resource association in Spring Data REST](https://stackoverflow.com/questions/25311978/posting-a-onetomany-sub-resource-association-in-spring-data-rest) – Hubert Grzeskowiak May 27 '17 at 12:25

2 Answers2

0

I found one solution where you have to post on the general item collection (in my case expenses) and put a reference to the containing entity into the payload:

POST http://localhost:8080/expenses

{
    "description": "new car",
    "comment": "way too expensive...",
    "amount": 3339,
    "creationDateTime": "2017-05-26T13:12:11.000+0000",
    "user":"http://localhost:8080/users/1"
}

Note the last line of the payload. It refers to the connected entity.

This solution works but it's not the way I would like it to work. I want users to post their own expenses, not on the general expenses table with a reference to themselves.

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
0

I think this is what you need.

Hope this help.

Erwin
  • 32
  • 4
  • Hi @Erwin. Thanks for trying to help. The huge article you linked describes the basic usage of Spring with REST. The problem I am having with `RepositoryRestResource` is slightly more specific. In fact the page doesn't even mention that annotation. Also note that link-only answers are discouraged on StackOverflow, because external pages can change or move anytime. If you find an answer on an external page, please quote the relevant part in addition to the link. – Hubert Grzeskowiak May 27 '17 at 14:18