Understanding data binding rules in JAX-RS

 

Understanding data binding rules in JAX-RS

While injecting variable values from the URI path and query parameter into the resource class or while mapping the request-response entity body with Java types, the JAX-RS runtime follows certain rules for the Java types present in the resource class.

Mapping the path variable with Java types

At runtime, the framework automatically detects and copies the parameter values present in the inbound request into the appropriate Java types based on the request parameter type. In general, the mapping is performed on the basis of the following rules for each of the request parameter annotation types, except for the @Context annotation:

  • All primitive types, such as shortintfloatdouble, and Boolean, except char.
  • All the wrapper classes of primitive types, such as short, integer, BigDecimal, and Boolean, except char.
  • All classes with a constructor that accept a single string type argument. In this case, you can define your own class with a single string type constructor and use it as a method parameter or member variable with an appropriate annotation for reading the parameter value.
  • Any class with the static method named valueOf(String) that accepts a single string argument.
  • Have a registered implementation of ParamConverterProvider JAX-RS extension SPI that returns a ParamConverter instance, capable of a from string conversion for the type.
  • If the parameters contain more than one value for the same name, you can have java.util.List<T>java.util.Set<T>, or java.util.SortedSet<T> as the Java variable type at the receiving end, where T represents the types that satisfy the first two criteria that we defined.

It would be interesting to see how the framework initializes the Java class types when there are no matching request parameters found. The following outcomes will occur if there is no matching value found in the request URI for a Java variable type and if @DefaultValue is not defined:

Now, let's see how the mapping is done between the request-response body content and Java types.

Mapping the request and response entity body with Java types


JAX-RS uses javax.ws.rs.ext.MessageBodyReader for mapping the HTTP request entity body to an appropriate Java type. On the other hand, javax.ws.rs.ext.MessageBodyWriter is used for mapping the Java type returned by a resource class method to the appropriate HTTP response entity body representation, such as JSON, XML, and text. The MessageBodyReader and MessageBodyWriter implementations will raise javax.ws.rs.WebApplicationException if they do not know how to convert the input data.

JAX-RS offers the default content handlers (entity providers) for all common data types. Here is a list of the Java types supported by JAX-RS by default:

  • JAX-RS supports mapping between the following Java data types and request-response entity bodies for all media forms: byte[]java.lang.Stringjava.io.InputStreamjava.io.Readerjava.io.Filejavax.activation.DataSource, and javax.xml.transform.Source
  • JAX-RS supports the javax.ws.rs.core.MultivaluedMap<K,V> type for reading or writing the form content, whose media type is application/x-www-form-urlencoded
  • JAX-RS supports the javax.xml.bind.JAXBElement type for reading or writing content represented using the XML media types (text/xmlapplication/xml, and application/*+xml)
  • JAX-RS supports the java.lang.Booleanjava.lang.Character, and java.lang.Number types for reading and writing the Boolean strings (true or false), characters, and numerical content presented in the text/plain media type


Comments

Popular posts from this blog

Understanding the JAX-RS resource life cycle

Generating a chunked output using Jersey APIs

Jersey client API for reading chunked input