Tuesday, January 18, 2011

Enabling RestEasy as JAX-RS Impl in Scalate

Scala as a language and its community is gaining a lot of attention, in fact, there are certain use cases where Scala offers incredible advantages over the traditional Java Language "as it is".

Although my experience with Scala so far is completely away from Web technologies, I've seen a lot of discussion about this matter, and a good solution for web applications can be basically a very simple composition:

  • HTML5 + REST Services [JSON]
  • HTML 5 + JavaScripts Toolkits such as JQuery and other + REST Service + [Many media types]

For the scenario that I mentioned above, I found out the framework Scalate, a good option for who is looking for a good getting started with Scala and Web.

 If you go in the Scalate's Getting Started Tutorial, everything works fine.  I spent the last couple of weeks researching a lot about which IDE to use, and at this moment honestly I recommend you use InteliJ Idea Community Edition with Scala plugins, it works incredibly fine and pleasant.

Well, but this post's title is about to enable RestEasy in Scalate Projects, so here will go deeper in this subject: Scalate by default comes with Jersey support implementation, which is another JAX-RS implementation, nevertheless for obvious reasons :) I prefer JBoss RestEasy, and in this post you will learn how you can change your scalate project to use RestEasy instead Jersey.

Everything I made is on my github online repository, I called this "version" of my scalate+resteasy project: easyscala, and you can checkout all the sources from here: https://github.com/edgars/easyscala

Changing the pom.xml

This is the first task you have to do, so you have to add RestEasy dependency in the pom.xml, although there is an important detail that I noticed when I tried to do that:
  • If you do not ignore the org.slf4j package, you will get an error, that I had not chance to go deeper to check the reason, that's why when I added RestEasy dependency I excluded the reference for this package, maybe some version conflicts of any other stupid thing that I had no time to check.




Changing the web.xml

Another change that I did, was in web.xml, where I removed the Jersey Servlet Filter's and any related configuration, and I added the RestEasy declaration as the following code:

     resteasy.scan     true

      resteasy.servlet.mapping.prefix      /resteasy



With a few changes, I was able to run the first REST Service made with JBoss RestEasy, look that we have a very standard JAX-RS Class in the following example:

package org.jboss.easyscala

import javax.ws.rs.{GET, Path, Produces}
import javax.ws.rs.ext.Provider

class RootService {

  @Produces(Array("text/xml", "application/json"))
  def getMediaType = "hello"

  def getHTML = "hello"

In the class above, we have 2 methods, according to the mime type (media type) that this service is requested, one of them will send the response. Another good point, at this moment, you cannot see any JBoss RestEasy proprietary extension. Obviously according your needs, you may add some helpers.


Scala and their related projects and solutions are gaining a lot of attention, it combines two programming models: OO and Functional, and this combination can make our codes easier, smaller and smarter. I cannot predict that much, but I am working in some specific Use Cases that were totally suitable for Scala, I am not talking about boilerplate code, but about less code with more power, and getting a huge difference when performance is a relevant issue. If you like JBoss tecnologies, like me, this is the first post where I will try show some good and relevant combinations.


daveclay said...

I've done some work with the Jackson project in developing the Jackson Scala Module, which can be used to handle Scala collections in RESTEasy.


Should be mainlined to jackson-module-scala master soon.

It requires a custom Provider to register the module:

@Consumes({MediaType.APPLICATION_JSON, "application/*+json"})
@Produces({MediaType.APPLICATION_JSON, "application/*+json"})
public class JacksonScalaContextResolver implements ContextResolver {

public ObjectMapper getContext(Class type) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ScalaModule());
return mapper;

Edgar Silva said...

Thank your for the contribution