RESTEasy 3 Atom Links Example with RESTServiceDiscovery, @AddLinks and @LinkResource
March 08, 2015
In this page we will learn how to inject atom links RESTEasy 3 in response. RESTEasy performs this task using RESTServiceDiscovery, @AddLinks and @LinkResource. RESTServiceDiscovery consists atom links and is defined in entity. @AddLinks helps to inject RESTServiceDiscovery in response to every entity and @LinkResource helps to find JAX-RS method by RESTServiceDiscovery to be included in atom links.
Atom Link
Atom link has href and rel attributes. Atom link works as reference from an entry or feed to web resource. It is declared as below.<atom:link rel="list" href="http://localhost:8080/concretepage-1/post/manage/articles"/>
RESTServiceDiscovery
org.jboss.resteasy.links.RESTServiceDiscovery discovers atom links and holds it. These atom links will be injected in response. RESTServiceDiscovery is defined in entity as a property. In the service class the JAX-RS method annotated with @AddLinks and @LinkResource are discovered as atom links. Find the entity class.Article.java
package com.concretepage; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlID; import javax.xml.bind.annotation.XmlRootElement; import org.jboss.resteasy.links.RESTServiceDiscovery; @XmlRootElement @XmlAccessorType(XmlAccessType.NONE) public class Article { @XmlAttribute private String author; @XmlID @XmlAttribute private String title; @XmlElementRef private RESTServiceDiscovery rest; public Article(){} public Article(String author, String title){ this.author = author; this.title = title; } }
@AddLinks
@AddLinks is applied on JAX-RS method if we want RESTEasy to inject RESTServiceDiscovery to every entity in the response.@LinkResource
We annotate JAX-RS method with @LinkResource if we want it to be discovered by RESTServiceDiscovery for atom links.Service Class
Find the service class using @AddLinks and @LinkResource annotations.ArticleService.java
package com.concretepage; import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import org.jboss.resteasy.links.AddLinks; import org.jboss.resteasy.links.LinkResource; @Path("/manage") @Consumes({"application/xml", "application/json"}) @Produces({"application/xml", "application/json"}) public class ArticleService { List<Article> list = new ArrayList<Article>(); { list.add(new Article("AAA", "Hibernate")); } @AddLinks @LinkResource(value = Article.class) @GET @Path("/articles") public Collection<Article> getArticles() { return list; } @LinkResource @POST @Path("/article") public void addArticle(Article article){ list.add(article); } @AddLinks @LinkResource @GET @Path("/article/{id}") public Article getArticle(@PathParam("id") Integer id) { return list.get(id); } @LinkResource @PUT @Path("/article/{id}") public void updateArticle(@PathParam("id") Integer id, Article article){ list.set(id, article); } @LinkResource(value = Article.class) @DELETE @Path("/article/{id}") public void deleteArticle(@PathParam("id") Integer id){ list.remove(id); } }
Application Class
Find the application class.ArticleApplication.java
package com.concretepage; import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; public class ArticleApplication extends Application { private Set<Object> singletons = new HashSet<Object>(); public ArticleApplication() { singletons.add(new ArticleService()); } @Override public Set<Object> getSingletons() { return singletons; } }
Output
Deploy the code and run the URL as http://localhost:8080/concretepage-1/post/manage/articles.We will get response in XML as below.
