-
Notifications
You must be signed in to change notification settings - Fork 4
Using the @Doc annotation
Links to human-readable documentation can be added to a json-home document using a docs
hint (see json-home draft specification). This hint is generated, if you add a @Doc
annotation with a link
attribute to the controller-class responsible for a link-relation type.
If you are planning to use the HtmlController to serve a HTML representation of your json-home document for developers, you possibly want to add some documentation to your link-relation types, var types, media types, and so on.
Using the @Doc annotation, you can:
- Add some short paragraphs to the description of a link-relation type.
- Link to external documentation.
- Link to local Markdown documents.
Links are added to the json-home as a docs
hint and may be rendered into the HTML representation.
In order to associate a link-relation type with a description, you could use the @Doc annotation like this:
@Doc(rel = "/rel/products",
value = {"The collection of products.",
"This is a second paragraph, describing the collection of products."}
)
@Controller
public class ProductController {
...
The annotation must be provided at type level, because there may be multiple methods in the controller using the link-relation type /rel/products
. The @Doc
annotation is always referring to a link-relation using the rel
attribute.
The value
of the annotation may contain one or more Strings. Each String is rendered as a paragraph.
You can add a link to some documentation instead of or in addition to the description provided with the value
attribute:
@Doc(value = "A link to a single product.",
rel = "/rel/product",
link = "http://de.wikipedia.org/wiki/Produkt_(Wirtschaft)"
)
In this case, an absolute link is provided. You can also use a relative link; in this case, the link will be transformed into an absolute link by prepending the value of the property jsonhome.relationTypeBaseUri
. See for details on how to use this.
The generated json-home of the link-relation type will contain a docs
hint like this:
{
"resources" : {
"http://specs.otto.de:8080/shop/rel/product" : {
"href-template" : "http://example.org/shop/products/{productId}",
"href-vars" : {
"productId" : "http://specs.otto.de/shop/rel/product#productId"
},
"hints" : {
"allow" : ["GET", "PUT"],
"docs" : "http://de.wikipedia.org/wiki/Produkt_(Wirtschaft)",
...
Many controllers will support more than one link-relation type. Because Java does not allow you to add one annotation multiple times to a type, we have to use the @Docs annotation to configure multiple @Doc like this:
@Controller
@RequestMapping(value = "/products")
@Docs({
@Doc(value = {"The collection of products.",
"This is a second paragraph, describing the collection of products."},
rel = "/rel/products"),
@Doc(value = "A link to a single product.",
rel = "/rel/product")
})
public class ProductsController {
In this example, documentation is provided for three link-relation types: the collection of products /rel/products
and a single product /rel/product
.
Please note that only one @Doc
per link-relation type is allowed. If your application contains multiple @Doc
annotations having the same rel
attribute, only one value will be used by the generators.