-
Notifications
You must be signed in to change notification settings - Fork 16
Message Body Providers
Message body readers and writers
-
javax.ws.rs.extMesageBodyReaderused for conversion of a stream to a Java type. There is prepared set of readers in EverRest framework (see table below).MessageBodyReadermay also have annotation@javax.ws.rs.Consumesto restrict media types for which it may be applied. IfMessageBodyReaderdoes not have such annotation it minds it can be applied to any type of content. -
javax.ws.rs.ext.MessageBodyWriterused for conversion of a Java type to a stream. There is prepared set of writers in EverRest framework (see table below).MessageBodyWritermay also have annotation@javax.ws.rs.Producesto restrict media types for which it may be applied. If there is no such annotation it minds it can be applied to any type of content.
There is unifying essence for MessageBodyReader and MessageBodyWriter in EverRest org.everrest.core.provider.EntityProvider.
public interface EntityProvider<T> extends MessageBodyReader<T>, MessageBodyWriter<T>
{
}Common way to deploy own readers and writers is using javax.ws.rs.core.Application to deliver it. Recommended by JSR-311 specification behaviour for all Providers is singleton, so as usual instances of readers and writers should be returned by method Application#getSingletons(). Per-request behaviour for readers or writers is also supported, see method Application#getClasses(). Per-request mode may be useful if reader or writer need access to request context. Example below shows how to add one resource and two providers MyPerRequestProvider and MySingletonProvider.
EverrestProcessor processor = new EverrestProcessor(new ResourceBinderImpl(),
new ApplicationProviderBinder(),
new DependencySupplierImpl(),
new EverrestConfiguration(),
new Application() {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> cls = new HashSet<Class<?>>(2);
cls.add(MyResource.class);
cls.add(MyPerRequestProvider.class);
return cls;
}
@Override
public Set<Object> getSingletons() {
Set<Object> objs = new HashSet<Object>(1);
objs.add(new MySingletonProvider());
return objs;
}
});
// processor is ready to process request to your resource(s).EverRest framework supports message bodies of following type:
| Content type | Java type |
|---|---|
*/* |
byte |
*/* |
javax.activation.DataSource |
application/xml,application/xhtml+xml,text/xml |
javax.xml.transform.dom.DOMSource |
*/* |
java.io.File (may be limited if access to file system is not allowed) |
application/x-www-form-urlencoded |
MultivaluedMap<String, String> |
multipart/* |
java.util.Iterator<org.apache.commons.fileupload.FileItem> |
*/* |
java.io.InputStream |
*/* |
java.io.Reader |
application/xml,application/xhtml+xml,text/xml |
javax.xml.transform.sax.SAXSource |
application/xml,application/xhtml+xml,text/xml |
javax.xml.transform.stream.StreamSource |
*/* |
java.lang.String |
application/json |
java.lang.Object (simple constructor, get/set methods) |
application/xml,application/xhtml+xml,text/xml |
javax.xml.bind.JAXBElement |
application/xml,application/xhtml+xml,text/xml |
java.lang.Object (simple constructor, get/set methods) |
JSON support is embedded in EverRest framework. It supports serialization/deserialization of Java Bean, arrays, collections of Java Beans. It also support Map<String, T>, where T is Java Bean object.
Since version 1.0.2 EverRest supports usage of interfaces of beans as method parameters. Also developer can use arrays, collections and maps of beans. If interface is found as method parameter and method consumes application/json media type then java.lang.reflect.Proxy created and passed in method. This can be very useful feature if GWT framework used for client side. Since version of 2.1.1 GWT supports http://code.google.com/p/google-web-toolkit/wiki/AutoBean AutoBean feature. So now developer can use the same interfaces at the server side to restore Java objects from JSON and not need to implement interfaces.
You can see example of using JSON in Java and Groovy based RESTful services .
You can easy create own providers by implementing interfaces javax.ws.rs.ext.MessageBodyReader<T> and javax.ws.rs.ext.MessageBodyWriter<T>. You are able 'override' embedded providers. It minds you providers always take preference over embedded providers with the same purposes (for the same media and Java type). For example you can easy use Jackson JSON provider in you application. EverRest framework will try to use your provider first and only if there is not corresponded provider in your application will try to use embedded one.