RESTful Web services on JBoss AS 7

In this sample tutorial we will show how to run RESTful Web services on JBoss AS 7 (release 7.0.2) using CDI and JPA to load persistent data.

NOTE: This article covers an old version of the application server. We recommend checking this tutorial which is up-to-date: Getting started with RESTEasy and WildFly

In order to use Web services in the new application server release you need the following extension in your configuration file:

<extension module=""/>

This extension is included by default since JBoss AS 7.1.1. In this example we will build a sample application which uses a RESTful Web services to gather some data which is returned in XML format. The first piece of our application will be the RESTful Web service which produces a list of Entities (of type Book):

package com.webservice;

import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import javax.persistence.Query;

public class RESTService  {
private EntityManager em;    
 public List<Book> listUsers() {
   Query query = em.createQuery("FROM com.entity.Book");
   List <Book> list =  query.getResultList();

   return list;

In this example, we are binding the Path to the specific context named “/list”.
You can apply the @Path annotation also at method level so that you can specify different virtual paths for your web services.

Now Let’s add the Entity class named Book to our project, which simply maps the table named “Book” on the database:

package com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;

public class Book {

 @Column(name = "name")
 String name;
 @Column(name = "isbn")
 String isbn;
 @Column(name = "price")
 int price;

public Book() {   }

public String getName() {
   return name;

public void setName(String name) { = name;

public String getIsbn() {
   return isbn;

public void setIsbn(String isbn) {
   this.isbn = isbn;

public int getPrice() {
   return price;

public void setPrice(int price) {
   this.price = price;


Next thing, as we have seen, this project uses Context Dependency Injection to inject the EntityManager into our project. In order to do that, we need a simple qualifier which exposes an entity manager using the resource producer pattern :

package com.qualifier;

import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class Resources {

 private EntityManager em;


After that, in order to activate CDI, you need to place the beans.xml file into the WEB-INF folder of your application, so that the deployer triggers CDI deployment. As we are not defining any beans into this file, just place add an empty file named beans.xml under WEB-INF.

Now the last step is activating our Web Service. This can be done in two ways: using XML configuration:

1) Define in web.xml that your service is a REST Application


2) Define a JAX-RS activator by adding a class which extends and defines an application path for your web service. [recommended]

package com.webservice;


public class JaxRsActivator extends Application {

That’s all. Remember to add a persistence.xml file to your META-INF folder of your application

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
 xmlns="" xmlns:xsi=""
   <persistence-unit name="persistenceUnit" transaction-type="JTA">
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

If you need a tutorial about installing a datasource on JBoss AS 7 you can find one here .

If you have correctly configured your application, you will end up with a project like this:

jboss 7 restful web services tutorial

The RESTful web service will be accessible using the path: http://localhost:8080/restas7/rest/list