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、概述
@Consumes
The comment represents a MIME type that a resource can accept.
@Produces
The 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
@Consumes
In @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
@DefaultValue
Such 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