Jersey (1.19.1)-Representations and Java Types

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.