Blog

Developing RESTful applications on JBoss AS 7

12 Jul, 2011
Xebia Background Header Wave

JBoss AS 7 CR1 has been released recently. On the previous release it was pretty easy to develop RESTful applications with the build in JAX-RS support based on RESTeasy. In this blog I’ll look at how well the new version of JBoss keeps up with the rest of the field.

Changes: JBoss Modules

JBoss AS 7 is quite a different beast from its predecessors. The most striking difference is the usage of JBoss modules, which is a modular class loading system, comparable to Jigsaw, THE modular system for Java that will (one day) (probably) reach the JVM. Till that day however, JBoss modules can be used.
After downloading and unzipping the JBoss AS 7 CR1 release, you’ll find all the modules in the ”modules” subdirectory, grouped in a structure that somewhat resembles a maven repository: you’ll for example find directories for ”org”, ”javax” and ”net”, similar to the directory structure of group-ids and artifact-ids in a Maven2 repository layout. At some level, however, there will be a ”main” folder and that’s where the implementation details of the module are kept: a small XML file describing the module and some jar files that constitute the modules content. A very useful side effect of this is that finding out which versions of which jars the AS is using is rather simple: The jar files in the module are usually the maven artifacts and thus contain the version number in their name. See for example the javax.activation module below.

Another consequence of this change is that the usual JBoss structure with a ”server” directory with several profiles, like ”default”, ”web” and ”minimal” no longer exists. The main structure now has a ”standalone” directory for running a simple server, and a ”domain” directory for running a cluster.

Installation

Installation is as always straight forward. A development environment however is not ready for use without good integration between the IDE and the application server on the local system. IDE of choice is Eclipse, because of the nice JBoss Tools plugin suite. Following these instructions is already a large part of the installation.
To make things really easy, we include Maven integration for Eclipse with WTP from Sonatype into the mix. Especially finding the right version of the Maven integration for WTP was difficult. I finally found a copy that worked with the rest of the setup at this update site.
Starting the server can now be done from the Eclipse servers window. Browsing to the default port shows a nice welcome window and going to the admin console shows a simple but useful administrative interface.

Two small problems with the setup so far:
1. Eclipse doesn’t seem to detect the startup of the server. Which is kind of annoying, because Eclipse now wont compile anything since it is waiting for the process to end.

This can be worked around by setting the Startup Poller to ”Web Port Poller” in the Server overview screen of the JBoss 7 AS server.

2. Eclipse is not able to stop JBoss. At least not from the server view.

JBoss can still be stopped from the Console view, but that means restarting the server always takes at least 4 mouse clicks: (1) go to console (2) stop (3) go to servers (4) restart, which is kind of annoying.
Anyway, living with small annoyances is second nature for everybody in IT industry I suppose, so why would a simple JEE application developer be any different 😉

Starting up the project

Now that everything is set up, we can continue to the more interesting part, the project and the Java code. With the help of M2Eclipse we create a maven project with the following pom:
[xml highlight=”41,42-47″]
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xebia.library</groupId>
<artifactId>library</artifactId>
<version>0.9-SNAPSHOT</version>
<packaging>war</packaging>
<repositories>
<repository>
<id>jboss</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>2.0.0.CR1</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
[/xml]
This pom will configure the maven project and the Eclipse-WTP project correctly for deployment on JBoss. The highlighted lines show the dependency on a pom with the Aggregated Java EE 6.0 APIs that JBoss provides. This pom is supposed to be used to import the right version numbers of all the Java EE APIs into your own pom. Since I am lazy, I’ll just depend on this pom, so all the APIs are already available in my project without having to specify them separately.
Now we have a well configured Maven Web project in Eclipse. The layout will look like the image below.

And now for some Java code, how do we configure RESTeasy framework?
[java title=”com/xebia/library/LibraryApplication.java”]
package com.xebia.library;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("services")
public class LibraryApplication extends Application {
}
[/java]
That’s it! From the class RESTeasy will be configured and the RESTful web services will be available at ”/services” in the context of the project.
Let’s add a simple web service.
[java title=”com/xebia/library/BookRepository.java”]
package com.xebia.library;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.xebia.library.model.Book;
@Path("books")
public interface BookRepository {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
List<Book> all();
}
[/java]
This interface defines the RESTful web service by means of the JAX-RS annotations. Adding an interface is useful, but not necessary.
[java title=”com/xebia/library/impl/BookRepositoryBean.java”]
package com.xebia.library.impl;
import java.util.Collections;
import java.util.List;
import com.xebia.library.BookRepository;
import com.xebia.library.model.Book;
public class BookRepositoryBean implements BookRepository {
@Override
public List<Book> all() {
return Collections.singletonList(new Book("My Title"));
}
}
[/java]
The implementation of the web service is very simple (for now) and it uses a very simple POJO as return value.
[java title=”com/xebia/library/model/Book.java”]
package com.xebia.library.model;
public class Book {
private String title;
public Book() {
}
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
[/java]
With these simple files, we have all we need to get a RESTful web service running on JBoss AS 7. Pointing the browser to the correct URL, we get a JSON string as shown below.

The URL is built up from a few parts:

  1. https://xebia.com/blog:8080 The JBoss web server
  2. library The application context
  3. services The path configured for the JAX-RS application in the @ApplicationPath annotation
  4. books The path for the BookRepository resource as given in the @Path annotation

JBoss AS 7 comes with RESTeasy (an implementation of JAX-RS) and Jackson (a Java-JSON serializarion framework) configured and that is how the final result is obtained. This makes JBoss AS 7 an ideal platform for developing RESTful applications: it works out-of-the-box and is build on well known open source projects, making it easy to adapt the container to specific requirements in you application.

Conclusion

In this blog we have set up a development environment for JBoss AS 7 using Maven, Eclipse and WTP and we deployed a very simple RESTful application. The setup works pretty well.
Keep an eye out for new blogs, in which I’ll improve on the current project to show more aspects of JBoss 7 and RESTful applications.

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts