AMZ
AMZ

Reputation: 362

Two questions about spring data rest

I have 2 questions about spring data rest.

First:

what is the difference between method and resource? Basically, what does resource mean in spring data rest?

Second:

basically, what is the reason that all CrudRepository methods are not exposed? for example, in the following link that is about the contacts repository(which extends CrudRepository) profile, there is not any supported operation for findAllById or existsById methods:

 {
        "id": "create-contacts",
        "name": "contacts",
        "type": "UNSAFE",
        "descriptor": [
          
        ],
        "rt": "#contact-representation"
      },
      {
        "id": "get-contacts",
        "name": "contacts",
        "type": "SAFE",
        "descriptor": [
          
        ],
        "rt": "#contact-representation"
      },
      {
        "id": "delete-contact",
        "name": "contact",
        "type": "IDEMPOTENT",
        "descriptor": [
          
        ],
        "rt": "#contact-representation"
      },
      {
        "id": "patch-contact",
        "name": "contact",
        "type": "UNSAFE",
        "descriptor": [
          
        ],
        "rt": "#contact-representation"
      },
      {
        "id": "get-contact",
        "name": "contact",
        "type": "SAFE",
        "descriptor": [
          
        ],
        "rt": "#contact-representation"
      },
      {
        "id": "update-contact",
        "name": "contact",
        "type": "IDEMPOTENT",
        "descriptor": [
          
        ],
        "rt": "#contact-representation"
      },
      {
        "name": "getAllByStatus",
        "type": "SAFE",
        "descriptor": [
          {
            "name": "status",
            "type": "SEMANTIC"
          }
        ]
      },
      {
        "name": "getAllByStatus",
        "type": "SAFE",
        "descriptor": [
          {
            "name": "status",
            "type": "SEMANTIC"
          }
        ]
      },
      {
        "name": "getAllByCreatedBy",
        "type": "SAFE",
        "descriptor": [
          {
            "name": "createdBy",
            "type": "SEMANTIC"
          }
        ]
      },
      {
        "name": "deleteById",
        "type": "SAFE",
        "descriptor": [
          {
            "name": "id",
            "type": "SEMANTIC"
          }
        ]
      }

Upvotes: 0

Views: 46

Answers (1)

BendaThierry.com
BendaThierry.com

Reputation: 2110

1/ In Spring Data Rest, a resource is a collection of results from a Repository.Official Documentation - Repository Resources is self-explanatory.

You'll get some explanations inside the documentation about methods. Methods are defined inside an interface which implements the Repository interface, and that's all folks if you are following the standard autocompletion with fields of the managed entity.

But from an HTTP point of view, resources and methods are different things :

  • An HTTP Method is GET, POST, PUT, PATCH, DELETE.
  • An HTTP Resource is the HTTP response when you are querying an URL, whatever it is. Don't mess things here.

2/ You must define all your Repositories with only the methods you need for your business model. If all of them are required by the default implementation of the framework, there will be a memory overloading, and that thing is useless, dangerous, not wishable in many memory optimized apps environments.

And if your point is only about the exposition of the list of all methods, check the visibility of your methods. Maybe the solution is here. Most of the time, only public ones are exposed, or only which of them implementing a public interface. Not necessarily the case in your code if your are implementing an interface of your business model.

And because they are all public by design, you can look at "Table 3. Ways to link to exported resources : Method" in https://docs.spring.io/spring-data/rest/docs/current/reference/html/

Upvotes: 1

Related Questions