JAX-RS specification-Analysis of common annotations

Source: Internet
Author: User

First, @Path

If you want a Java class to handle rest requests, this class must add at least one @path ("/") annotation;

For a method, this annotation is optional and, if not added, inherits the definition of the class.

(1) The value in the @Path can be a complex expression, such as @path ("{ID}"), where {ID} expression code has a template parameter;

A template parameter is a wildcard character defined in the @path, which begins with {, the middle is a bunch of mixed strings of letters and numbers (cannot contain/characters), ending with a}. Also such as: @Path ("{firstname}-{lastname}").

(2) @Path also supports regular expressions, such as @Path ("{ID: \\d+}"), where \\d+ represents a number. Format is: A:B


Second, @Method

(1) Only a annotation of an HTTP method, such as @get, can be qualified to process a request.

(2) for the specific operation type of the resource, it is represented by the HTTP verb.

The usual HTTP verbs have the following five (the corresponding SQL command in parentheses).

  • GET (SELECT): Remove resources from the server (one or more items).
  • POST (Create): Creates a new resource on the server.
  • PUT (UPDATE): Updates the resource on the server (the client provides a full resource after the change).
  • PATCH (UPDATE): Updates the resource on the server (the client provides changed properties).
  • Delete: Deletes a resource from the server.

  • head: Gets the metadata for the resource.
  • options: Gets information about which properties of a resource are available to the client to change.

Here are some examples.

  • Get/zoos: List all Zoos
  • Post/zoos: Create a new zoo
  • GET/ZOOS/ID: Get information about a given zoo
  • PUT/ZOOS/ID: Update information for a designated zoo (all information about the zoo)
  • PATCH/ZOOS/ID: Update information for a given zoo (some information about the zoo)
  • DELETE/ZOOS/ID: Delete a zoo
  • Get/zoos/id/animals: List all animals in a designated zoo
  • DELETE/ZOOS/ID/ANIMALS/ID: Delete a designated animal from a designated zoo


Three: The difference between accept and Content-type

accept represents the data format that the interface will return to the client.
Content-type represents the data format that the client sends to the server side. This is defined when writing the rest interface.Normal If the server does not define accept but added the words will be reported 404 did not find the corresponding interface.


Four: @Produces represents the MIME data type returned by the class or method .

There are several formats as follows:

(1) @Produces ("Text/plain") text type

(2) @Produces ("text/html") HTML type

(3) @Produces ({"Application/xml"}) XML type

(4) @Produces ({"Application/json"}) JSON type

You can annotate two or more MIME types at one time, such as {"Application/xml", "Application/json"} which means both can be used, but the choice is usually the former, that is, Application/xml, Because it appears for the first time.


V: @Consumes represents a MIME type that a resource can accept.

@Consumes Note for Content-Type the request header is matched to determine whether the method can accept the contents of a given request.

vi: @Queryparam and @Pathparam difference

< Span style= "FONT-FAMILY:HELVETICA,TAHOMA,ARIAL,SANS-SERIF; line-height:25.1875px ">@< Span style= "FONT-FAMILY:HELVETICA,TAHOMA,ARIAL,SANS-SERIF; line-height:25.1875px ">queryparam: Specifies that the parameters in the URL are in the form of a key-value pair and   in the program; @QueryParam (" from ")  int read out the value of from in the URL,  

< Span style= "FONT-FAMILY:HELVETICA,TAHOMA,ARIAL,SANS-SERIF; line-height:25.1875px "> For example: Span style= "FONT-FAMILY:HELVETICA,TAHOMA,ARIAL,SANS-SERIF; line-height:25.1875px ">url input: users?from=100&to=200&orderby=age&orderby=name 

@Pathparam:Only the value of the parameter appears in the URL, no key-value pairs appear

Example:/users/100



JAX-RS specification-Analysis of common annotations

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.