Previous sections on @Produces and @Consumes referred to MIME media types of representations and showed resource methods T Hat consume and produce the Java type String for a number of different media types. However, String is just one of the many Java types that was required to being supported by JAX-RS implementations.
Java types such as byte[], Java.io.InputStream, Java.io.Reader and Java.io.File are supported. In addition JAXB beans is supported. Such Beans is jaxbelement or classes annotated with @XmlRootElement or @XmlType. The samples JAXB and JSON-FROM-JAXB show the use of JAXB beans.
Unlike method parameters that is associated with the extraction of request parameters, the method parameter associated WI Th the representation being consumed does not require annotating. A maximum of one such unannotated method parameter may exist since there could be a maximum of one such representation Sent in a request.
The representation being produced corresponds to what's returned by the resource method. For example Jax-rs makes it simple to produce images that is instance of as File
follows:
@GET @path ("/images/{image}") @Produces ("Image/*") PublicResponse GetImage (@PathParam ("image") (String image) {if(!issafetoopenfile (image)) { Throw NewIllegalArgumentException ("Cannot open the image file.")); } File File=NewFile (image); if(!file.exists ()) { Throw NewWebapplicationexception (404); } String MimeType=NewMimetypesfiletypemap (). getContentType (file); returnResponse.ok (file, MimeType). Build ();}
A File
type can also be used when consuming, a temporary file would be created where the request entity is stored.
The Content-type (if not set, see next section) can is automatically set from the MIME media types declared by @Produces I f The most acceptable media type isn't a wild card (one that contains a *, for example "application/" or "/*"). Given the following method:
@GET @produces ({"Application/xml", "Application/json"}) Public String Dogetasxmlorjson () { ...}
If "Application/xml" is the most acceptable then the of the response would be Content-Type
set to "Application/xml".
Jersey (1.19.1)-Representations and Java Types