Home  >  Web Services  >  RESTEasy 3

RESTEasy 3 Atom Links Example with RESTServiceDiscovery, @AddLinks and @LinkResource

By Arvind Rai, 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"/>


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.
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;
public class Article {
    private String author;
    private String title;
    private RESTServiceDiscovery rest;
    public Article(){}
    public Article(String author, String title){
    	this.author = author;
    	this.title = title;


@AddLinks is applied on JAX-RS method if we want RESTEasy to inject RESTServiceDiscovery to every entity in the response.


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.
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;
@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"));
    @LinkResource(value = Article.class)
    public Collection<Article> getArticles() {
       return list;
    public void addArticle(Article article){
    public Article getArticle(@PathParam("id") Integer id) {
    	return list.get(id);
    public void updateArticle(@PathParam("id") Integer id, Article article){
    	list.set(id, article);
    @LinkResource(value = Article.class)
    public void deleteArticle(@PathParam("id") Integer id){

Application Class

Find the application class.
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());
    public Set<Object> getSingletons() {
        return singletons;


Deploy the code and run the URL as http://localhost:8080/concretepage-1/post/manage/articles.
We will get response in XML as below.
RESTEasy 3 Atom Links Example with RESTServiceDiscovery,  @AddLinks and  @LinkResource

Download Complete Source Code


©2022 concretepage.com | Privacy Policy | Contact Us