Jacob Orshalick is a software consultant, open source developer, and author. He is the owner of solutionsfit, a consulting firm dedicated to aligning businesses with technology. His software development experience spans the retail, financial, real estate, media, telecommunications, and health care industries. Jacob is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

Java and Rails integration with JAX-RS and ActiveResource

10.05.2011
| 3684 views |
  • submit to reddit

ActiveResource makes it easy to integrate Rails applications through RESTful services, but what if the resource is being produced by a Java application. JAX-RS is the best bet for writing these resources and with a few tricks we can get a service to conform to the requirements of an ActiveResource service. Read on to see that integrating your Rails and Java applications is easier than you think.

What ActiveResource expects

ActiveResource has several conventions it follows that make it easy to produce and consume RESTful services between Rails applications. The easiest way to communicate with ActiveResource is to follow these conventions. The following conventions are expected:

  • The type of the attribute must be specified as an XML attribute (otherwise the type is String)
  • Class names and attributes follow a dash convention for multiple words (e.g. my_attribute would be my-attribute in XML)
  • Resource paths follow the underscore convention for the class name and the class name should be pluralized (e.g. if we’re creating a resource for MyResource the base path should be /path_to_services/my_resources)

JAX-RS provides the ability to customize XML marshalling by extending the XmlAdapter class. This class simply requires that you provide the logic to marshal and unmarshal your object in the required fashion. ActiveResource uses element attributes to specify type information.

For example, if we have a MyApp class with an integer representing the number of Rails clients. The XML expected by ActiveResource would be:

<my-app>
  <id type="integer">5</id>
  <name>My Cool App</name>
  <rails-clients type="integer">1</rails-clients>
</my-app>

There are a couple of things to note here. ActiveResource follows a dash convention for class names and attributes that contain multiple words. The ActiveResource class would be as follows:

class MyApp < ActiveResource::Base
  self.site = 'http://solutionsfit.com/services/

  schema do
    attribute 'id', :integer
    attribute 'name', :string
    attribute 'rails_clients', :integer
  end
end

Notice also the use of the type="integer" attribute for the XML elements. This ensures that the id and rails_clients attributes are of boolean type. In addition, by convention, the path to this resource based on the self.site definition above would be: http://solutionsfit.com/services/my_apps.

Defining a JAX-RS XmlAdapter

To achieve this, we can create an XmlAdapter class in our Java application:

public class ActiveResourceIntegerAdapter
    extends XmlAdapter {
  @Override
  public ActiveResourceInteger marshal(Integer i)
      throws Exception {
    return new ActiveResourceInteger(i);
  }

  @Override
  public ActiveResourceInteger unmarshal(ActiveResourceInteger i)
      throws Exception {
    return i.getValue();
  }
}

The next step is to define the ActiveResourceInteger class which specifies how the marshalling should occur:

public class ActiveResourceInteger {
  private String type = "integer";
  private Integer value;

  public ActiveResourceInteger() {}

  public ActiveResourceInteger(Integer value) {
    this.value = value;
  }

  @XmlAttribute
  public String getType() {
    return type;
  }
  public void setType(String type) {
    this.type = type;
  }
  @XmlValue
  public Integer getValue() {
    return value;
  }
  public void setValue(Integer value) {
    this.value = value;
  }
}

Creating the JAX-RS Resource

Creating the JAX-RS resource is simple once we have defined our JAX-RS extension. First create the resource in the normal fashion:

@Path("/services/my_apps")
@Produces("application/xml")
public class MyAppResource {
  @GET
  @Path("/{myAppId}")
  public MyApp getMyApp(@PathParam("myAppId") Integer myAppId) {
    // retrieval logic for getting and returning MyApp instance
  }
}

Notice that we follow another ActiveResource convention here by pluralizing my_apps in the @Path definition. Now let's look at the MyApp definition:

@XmlRootElement(name="my-app")
public class MyApp {
  private Integer id;
  private String name;
  private Integer railsClients;

  @XmlElement(name="id")
  @XmlJavaTypeAdapter(ActiveResourceIntegerAdapter.class)
  public Integer getId() {
    return this.id;
  }
  public void setId(Integer id) {
    this.id = id;
  }

  @XmlElement(name="rails-clients")
  @XmlJavaTypeAdapter(ActiveResourceIntegerAdapter.class)
  public Integer getRailsClients() {
    return this.railsClients;
  }
  public void setRailsClients(Integer railsClients) {
    this.railsClients = railsClients;
  }

  // ... ...
}

Here we simply add the @XmlJavaTypeAdapter annotation with our adapter class specified to the integer attributes. We also used the dash convention when specifying the root element name and the attribute names.

Now when your class is marshalled to XML it will follow the conventions required by ActiveResource ensuring that your attributes are named and typed as expected. These adapters can be created for each type to avoid the default String type for your ActiveResource class attributes.

 

From http://solutionsfit.com/blog/2011/09/25/java-2-rails-with-jax-rs-and-activeresource/

Published at DZone with permission of Jacob Orshalick, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: