javax.? Ws.? Rs

Source: Internet
Author: User

 public  class   Restjaxrsserver { public  static  void  main (string[] args) throws    Exception {Component Component  = new   Component ();        Component.getservers (). Add (Protocol.http,  8009 new  restjaxrsapplication (null   "The Restlet server started ..." 
 public  class  restjaxrsapplication Span style= "COLOR: #0000ff" >extends   jaxrsapplication { public   Restjaxrsapplication (context context) { super   this . Add (new      MyApplication ()); }}
 public  class  myapplication extends   application {@Override  public  set<class<?>> getclasses () {Set  <class& lt;? >> resources = new  hashset<class<?>>                 (); Resources.add (Studentresource.  class          class          class                  return   resources; }    }
 @Path ("/test"  public  class   Testresource {  @GET @Path ( "test"    @Produces ("text/html")   public  String Test (@QueryParam ("name") string name, @DefaultValue ("Ten") @QueryParam ("Age") int  age) throws   interruptedexception {System.        Out.println ( "name=" +name);        System.out.println ( "age=" +age);     return  "SUCCESS" ; }    }

1、概述

@ConsumesThe comment represents a MIME type that a resource can accept.

@ProducesThe comment represents a MIME type that a resource can return.

These annotations can be found within a resource, resource method, child resource method, sub-resource locator, or sub-resource.

Several common annotations

@Cookieparam

@FormParam

@HeaderParam

@MatrixParam

@PathParam

@QueryParam

@BeanParam

a total of 7 annotations.

Here I first introduce the following three annotations

@MatrixParam

@PathParam

@QueryParam

All three annotations are taken from a URL .

@MatrixParam take the URL semicolon ( the argument after the;). This also must pay attention, must be in ? Front of the number.

@PathParam Take the parameters in the URL path. When you use it, be aware that you want to put a variable in the @Path annotation, enclosed in {} , before you can use it.

@QueryParam fetch URL? The following request parameters.

2.@Produces: Type returned

A. Returning to the client string type (Text/plain)

@Produces (Mediatype.text_plain)   

B. Return to client as JSON type (Application/json)

@Produces (Mediatype.application_json)   

3, @Consumes

@ConsumesIn @Produces contrast, it is used to specify the MIME type that can be accepted by the client and can also be used for class or method, or multiple MIME classes can be specified.

Type, generally used @PUT , @POST

A. Accept the client parameter as a string type

@Consumes (Mediatype.text_plain)

B. Accept the Clent parameter as JSON type @consumes (Mediatype.application_json)

Other annotations:

@PathParam
    @GET        @Path ("{username"})        @Produces (Mediatype.application_json)        public User GetUser (@PathParam ("username") String username) {            ...        }    
@QueryParamgets the query parameters in the GET request:
    @GET        @Path ("/user")        @Produces ("Text/plain")        Public user GetUser (@QueryParam ("name") String name,                             @QueryParam (int age ) {            ...        }   
If you need to set a default value for a parameter, you can use @DefaultValueSuch as:
    @GET        @Path ("/user")        @Produces ("Text/plain")        Public user GetUser (@QueryParam ("name") String name,                            @DefaultValue (int age ) {            ...        }  
@FormParamget the data from the form in the POST request:

    @POST        @Consumes ("application/x-www-form-urlencoded")        publicvoid Post (@FormParam ("name") String name) {            //  Store the message        }   

@BeanParam Gets the data in the request parameter, encapsulated with the entity Bean

    @POST        @Consumes ("application/x-www-form-urlencoded")        publicvoid  Update (@BeanParam user user) {            //  Store The user data        }    

 


javax.? Ws.? Rs

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.