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.

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

<extension module="org.jboss.as.webservices"/>

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;
import javax.ws.rs.*;
 

@Path("/list")
public class RESTService  {
 
@Inject
private EntityManager em;    
 
@GET
@Produces("text/xml")
 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;

@XmlRootElement
@Entity
public class Book {

 @Id
 @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) {
   this.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 {

 
 @SuppressWarnings("unused")
 @PersistenceContext
 @Produces
 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

<servlet>
   <servlet-name>Resteasy</servlet-name>
     <servlet-class>
       org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
     </servlet-class>
     <init-param>
       <param-name>javax.ws.rs.Application</param-name> 
       <param-value>com.webservice.RESTService</param-value>
    </init-param>
 
 </servlet>  

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

package com.webservice;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
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="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="persistenceUnit" transaction-type="JTA">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <jta-data-source>java:/MySqlDS</jta-data-source>  
 
     <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
     </properties>
  </persistence-unit>
</persistence>

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

Found the article helpful? if so please follow us on Socials