Java Reference
In-Depth Information
RESTful Web Services and Attachments
RESTful web services that implement
Provider<Source>
cannot return arbitrary
MIME-typed data (e.g., a JPEG image). They can only return XML messages with
no attachments. If you want to return an attachment (such as an image file), your
web service class must implement the
Provider<DataSource>
interface; the
javax.activation.DataSource
interface provides the JavaBeans Activation
Framework with an abstraction of an arbitrary collection of data.
Listing 11-23
presents an Image Publisher RESTful web service that demonstrates
how you could use
DataSource
with two other
javax.activation
package
types to return a JPEG image to a client.
Listing 11-23.
Returning a JPEG image in response to a GET request
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.annotation.Resource;
import javax.xml.ws.BindingType;
import javax.xml.ws.Endpoint;
import javax.xml.ws.Provider;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.WebServiceProvider;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.http.HTTPBinding;
import javax.xml.ws.http.HTTPException;
@WebServiceProvider
@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE)