One of the benefits of providing users with REST resources is that the client does not need to have prior knowledge of the links other than the initial URI. Instead, the client only has to know about the links provided by the server to navigate from one resource to another. With the exception of the context root resource, all resources should be accessible through different resources.
When you create a resource, it should always contain a link to itself. This current resource link is known as the self
link, which determines how to access the resource. It is best to use canonical links for a resource, which are added by default.
Links should consist of a link relation name, a URL and any other optional attributes. Links for every endpoint in the resource are automatically generated for representation responses. The link uses a linkRelation
that you can define using the @Endpoint
annotation linkRelation
attribute. If an attribute is not specified, the system automatically generates a link relation from the endpoint method’s name. For example, the link relation for a SubmitOrder
endpoint might be:
@Endpoint(linkRelation="SubmitOrder")
By adding a self
link, you ensure that relative links can be resolved and that responses from the client can be returned to the server.
The selfLinkFromContext
method of the LinkUtils
provides a way to generate a self
link based on the data in context. If an endpoint needs a custom self link, it can call this method. Refer to the Customizing Self Links section for additional information.