ZapFlash

Shoehorning Java into RESTful Design

What are the primary reasons you code in Java in your enterprise IT shop? Be honest now:

a)      We have solid Java programming skills on staff

b)      We have a lot of existing Java in production we’ve coded over the years and/or we’ve made a significant investment in Java-centric infrastructure (ESBs, application servers, etc.)

c)      Of all the languages and programming and execution environments we could have chosen, Java is the best suited for addressing our specific bespoke software challenges, independent of current skill sets, infrastructure, or existing production code.

If you selected a and/or b but not c, then you have what we like to call a legacy Java problem. There are other, perhaps more modern languages that would better suit your needs, but  because of your existing investments in people and technology, you’ve essentially shoehorned Java.

All is not lost, however. The ongoing efforts of the Java Community Process (JCP) have augmented the capabilities of the Java ecosystem in order to tackle a wide range of new problems and situations. This ongoing work on Java may continue to meet your needs for years into the future.

But then again, maybe not. The heavyweight model-view-controller (MVC) architecture of Java EE, or simply the object-oriented context of Java more broadly may not support alternative architectural approaches. In particular, if you are building a distributed hypermedia application, then a RESTful architectural approach will likely better meet your needs than a Java-centric architectural approach.

JAX-RS to the Rescue?

But wait, you Java geeks out there are saying, can’t we have both? Wouldn’t it be ideal if we had a Java-centric way of building RESTful applications? After all, the JCP has worked to address Java’s support for REST, which led them to the Java APIs for RESTful Services (JAX-RS) standard. Now we can build RESTful applications simply and easily in Java, right?

Unfortunately, JAX-RS is a classic example of shoehorning. It adds some RESTful capabilities to Java, but fights the RESTful architectural style every step of the way. Case in point: the Hypermedia as the Engine of Application State (HATEOAS) constraint. JAX-RS 1.1 didn’t support hypermedia at all, which essentially meant that we would have to simulate HATEOAS characteristics by utilizing core Java capabilities. To make JAX-RS fully RESTful, you would have to design applications that satisfied all of REST’s architecture constraints, compensating for those features that did not have adequate support during development. JAX-RS 1.1 provides adequate support for satisfying some REST architecture constraints, but punts on HATEOAS.

JAX-RS is an adequate API for implementing RESTful Services in Java, but JAX-RS version 1.1 has no built-in support for hypermedia. In JAX-RS 2.0, expands hypermedia support. The JCP has added hyperlinks and client support to the forthcoming JAX-RS 2.0, but even so, developers will find JAX-RS an immature tool for building distributed hypermedia systems.

More Challenges with JAX-RS

To simplify the development and deployment of RESTful Services, JAX-RS uses Java annotations. There are Java annotations to express REST resources, media types, access methods, etc. There are no annotations to express hyperlinks. You have to design those yourself.

As another example, consider standardized Service contract and loose coupling design principles. RESTful Service contracts require the unification and standardization of the resource identifier syntax and conventions for expressing and accessing resources. In the case of RESTful Services, you should design URIs in a consistent way. Furthermore, URIs should be opaque to reduce coupling between consumers and RESTful services. JAX-RS allows such consistency, but doesn’t encourage or enforce it.

It’s also important to standardize custom media types whenever possible, and you must also select which standard media types and custom media types you should use. Furthermore, the Service discoverability design principle requires having hypermedia controls in place, based on the REST HATEOAS design constraint. Discoverability provides a way of making the protocol self-documenting. JAX-RS doesn’t help with either of these design principles.

As a final example of shoehorning, consider the two different types of links that JAX-RS defines:  transitional links which describe optional next actions and structural links  which provide optional detailed information. Transitional links tell a client where to proceed next, while structural links help to shorten representations in order to avoid aggregate data. Details are replaced by links. Transitional links have some support in JAX-RS 2.0, but structural links are not supported due to their level of complexity.

So, why have structural links in the first place? The answer: we’re trying to shoehorn the Java object structure into hypermedia. If we simply take a Java object and convert it into hypermedia, we end up with links that represent all the various method calls on the associated object instances, resulting in excessively large, complex representations. Instead, JAX-RS encourages transitional links which enable the client to drill down to the underlying aggregate data. But if we weren’t handcuffed by the Java object structure in the first place, we’d never bother with transitional and structural links. Instead, we’d design our hypermedia applications to support discoverability. In other words, a core Java pattern becomes a hypermedia antipattern.

The ZapThink Take

It’s quite common for developers to rush to developing Services without first understanding the design of their final product. It is especially important when we are developing RESTful Services to have a well-architected, comprehensive design for developing Services that follow RESTful architectural constraints. However, to develop a good RESTful Service takes more than a good development tool or coding best practices accumulated over time.

People are often confused while developing with JAX-RS and thinking REST. JAX-RS is not REST design; rather, JAX-RS should support good RESTful design. Furthermore, the use of JAX-RS API does not lead to the creation of fully RESTful Services unless you have completed proper RESTful design beforehand. The design has to ensure that you have satisfied all RESTful architecture constraints.

Finally, established SOA governance should include policies and processes related to the endorsement of RESTful architecture principles. Those policies should cover design requirements and policies that you have established for satisfying RESTful design principles. But without adequate architecture, all the coding in the world won’t lead to proper governance.

About Leonid Felikson

Leonid Felikson has over 25 years of experience in software design, development, and implementation of large scale enterprise distributed systems, including consultation for both the government and private sectors. He has a diversified background in Information Systems and Enterprise Architecture. For the past 12 years, his focus has been on distributed computing; systems analysis, design, and integration; web services patterns and technologies; and service-oriented architecture and practices.

Mr. Felikson has extensive hands-on experience in product development and architecture in the software industry and he is a named expert in the field of distributed computing. He is a thought leader in providing the best solutions in IT strategy and in developing and utilizing SOA adoption enterprise-scale programs and SOA roadmaps. He is an innovator in establishing practices of modeling, designing, developing, and deploying services, performing both leadership and technical roles in delivering enterprise-scale applications and business process integration implementations.

Mr. Felikson devotes his time to knowledge sharing and education, mentoring and evangelizing SOA concepts and SOA practices. He has taught courses and seminars on the principles of operating systems and computer programming, as well as database concepts and design, data structures and algorithms, and software architecture and methodologies at a variety of public and private organizations.

Mr. Felikson has extensive teaching experience, working with a variety of schools and colleges, including serving as faculty at Johns Hopkins University for the last 11 years. He has developed curricula and taught courses within the CS and IST graduate programs at JHU. Since 2005, Mr. Felikson has been a contributing member of WS-I (www.ws-i.org), participating and contributing to the development of the WS-I Basic Profile 1.2 and the WS-I Reliable Secure Profile 1.0. Mr. Felikson received his B.S. and M.S. in Computer Science from the Ural Polytechnic Institute, in Russia.

Mr. Felikson is a Licensed ZapThink Architect and Certified ZapThink Trainer, and will be conducting Licensed ZapThink Architect courses in 2013.

Photo credit: David Pursehouse

Discussion

5 comments for “Shoehorning Java into RESTful Design”

  1. Mr. Felikson writes, “There are other, perhaps more modern languages that would better suit your needs, but because of your existing investments in people and technology, you’ve essentially shoehorned Java.”

    In all seriousness, what language would you suggest as a replacement for Java when building systems with a RESTful approach?

    I understand that Java is getting “long in the tooth,” but I firmly believe in the benefits of strong typing and other programming practices that Java embodies. Would you consider Scala (the next logical evolution of Java) more suitable? If not Scala either, then what would you suggest?

    Posted by John J. Szucs | November 4, 2012, 8:50 am
    • John, I appreciate you asking this question. Just like you, I prefer strong typing in coding. When it comes to selecting programming language, of course, there are lots of languages that can be used for developing REST applications, and each one has pros and cons in terms of RESTful implementation. I don’t favor any one language over another here simply because I firmly believe that any programming language can be advantageous as long as it provides adequate support for implementing REST compliant architecture. Of course, architecture will have certain influence of the language of choice. But more important here is focusing on designing application to conform to REST design constraints.

      Posted by Leonid Felikson | November 8, 2012, 11:22 am
  2. RESTful Service contracts require the unification and standardization of the resource identifier syntax and conventions for expressing and accessing resources

    Posted by Wallace Ortega | November 5, 2012, 12:56 am
  3. As I’ve worked on RESTful systems e.g. using XML/XSLT/Cocoon (a few years ago), or lots of JSON services (more recently), with java I’ve observed something like the old “object relational mismatch” but in the realm of the resource representations.

    i.e. If you’re focused on *objects* from your problem domain and designing your code as basically a *simulation* of those then I think OO can be really helpful. So buttons in a GUI, characters in a game, etc. make for great “objects”.

    But in these RESTful systems I’ve worked on, the “resource representations” (as xml or json documents) were what was truly central to the design, *not* “objects” in the classic OOD sense of “modeling” or “simulating” objects from the real world.

    What we found in this recent JSON services project was that java was adding a lot of extra (and not very readable) code that was really only there to convert between the JSON and the java objects and back again. We wound up using server side javascript (rhino/ringo) and this greatly simplified our code (reduced line counts and improved readability so we could see the JSON resource representations easily in the code).

    I think the attention NoSQL DBs like mongo and server side javascript frameworks like node.js are getting illustrate how java’s traditional OOD approach may be a poor fit for a lot of RESTful systems, esp. if they use JSON.

    Posted by Darren Cruse | November 25, 2012, 1:48 pm
  4. HATEOAS gets talked about a lot but not implemented a lot. I’ve yet to see good examples of REST clients (excluding browsers of course) making use of hypermedia. This isn’t a JAX-RS problem.

    This has been discussed before: http://www.subbu.org/blog/2007/12/hypermedia-and-rest.

    Posted by Chuck M | November 30, 2012, 2:37 pm

Post a comment

FREE POSTERS

NEW VERSION! ZapThink's Vision for Enterprise IT in 2020
With all new content including Dev/Ops, Hypermedia-Oriented Architecture, Big Data Visualization, and more!
Click here to download for FREE
10-pack of prints for only the cost of shipping!

SOA Implementation Roadmap
Over 100,000 downloaded!
Click here to download for FREE
10-pack of prints for only the cost of shipping!