DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Java and Rails integration with JAX-RS and ActiveResource

Java and Rails integration with JAX-RS and ActiveResource

$$anonymous$$ user avatar by
$$anonymous$$
·
Oct. 06, 11 · Interview
Like (0)
Save
Tweet
Share
4.69K Views

Join the DZone community and get the full member experience.

Join For Free

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/

Java (programming language) Attribute (computing) Integration application XML Data Types Element Strings REST Web Protocols

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is JavaScript Slice? Practical Examples and Guide
  • Comparing Map.of() and New HashMap() in Java
  • Specification by Example Is Not a Test Framework
  • Distributed Tracing: A Full Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: