JAX-RS Response Entity Providers - No Fluff Just Stuff

JAX-RS Response Entity Providers

Posted by: Brian Gilstrap on July 9, 2009


[Note: This is one in a series of posts about JAX-RS. You may wish to start at the beginning]

If you read my previous entry about returning more than one representation of a resource, you saw this sample code for a RESTful F-22 fighter plane resource:
@Path("/planes/f22/")
public class F22 {
@GET @Produces("text/html") @Path( "f22.html" )
public File getHTMLRepresentation() throws IOException {
return new File( "f22.html");
}

@GET @Produces("image/jpeg") @Path( "f22.jpg" )
public File getImageRepresentation() {
return new File( "f22.jpg");
}
}

These methods might seem a bit odd at first sight. The getHTMLRepresentation method has a @Produces annotation which indicates the method produces a MIME type of "text/html", but the return type of the Java method is java.io.File. Similarly, the getImageRepresentation method produces "image/jpeg" but the Java method also returns a File.

This apparent disconnect is resolved by a JAX-RS feature called entity providers. The specification states that entity providers "supply mapping services between representations and their associated Java types". The job of entity providers is to take HTTP request entities and turn them into Java objects on the request side, and take Java objects and turn them into response entities on the result side. Entity providers which convert request entities to Java types implement the MessageBodyReader interface and must be marked with the @Provider annotation. Similarly, entity providers which convert Java return types to response entities implement the MessageBodyWriter interface and are marked with the @Provider annotation (see Figure 1).


Figure 1: Conversion between HTTP entities and Java objects

JAX-RS mandates a set of built-in entity providers covering common entity types such as strings, input streams, java.io.Reader objects, java.io.File objects, and a number of other sophisticated types of conversions.

In this case, Jersey takes the returned File object and uses it’s contents as the response entity which it marks as “text/html”. This mapping from Java types to HTTP entities can be quite powerful, as you don’t have to write code to read the contents of files and return them as byte arrays, Strings, etc. It also offers a JAX-RS implementation the opportunity to implement handling of some types much more efficiently. For example, a JAX-RS implementation might choose to use Java’s NIO facilities to read the contents of a file for performance reasons.

This notion of entity providers is a crucial aid to the POJO (Plain Old Java Objects) style of JAX-RS applications, and avoids what would otherwise be a great deal of boilerplate code in the application performing these conversions.

Brian Gilstrap

About Brian Gilstrap

Brian Gilstrap is a Principal Software Engineer at Object Computing, Inc. where he has spent the last eleven of his 20+ years in the industry. In those years, he has worked with many languages and many technologies. He writes and blogs frequently, and has been on the steering committee of the St. Louis Java User's Group more than a decade. With OCI he provides consulting to companies in many industries and countries, and develops & delivers training courses for Washington University's Center for Applied Information Technology.

Brian has a passion for building software that is easy to use and robust while still meeting the rapid development requirements in today's industry. He has expertise in distributed systems, object oriented analysis and design, secure computing, and many languages and frameworks.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »