Understanding data binding rules in JAX-RS
Understanding data binding rules in JAX-RS
Mapping the path variable with Java types
- All primitive types, such as short, int, float, double, 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:
- All the primitive Java types follow the default value rules set for the primitive types in Java, which are listed at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- All Java objects will be set to null
- List, set, or SortedSet will have a corresponding empty collection instance
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.String, java.io.InputStream, java.io.Reader, java.io.File, javax.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/xml, application/xml, and application/*+xml)
- JAX-RS supports the java.lang.Boolean, java.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
Post a Comment