One: Rest Introduction
REST 2000, presented by Roy Fielding in his doctoral dissertation, is one of the chief authors of the HTTP Specification version 1.0 and 1.1.
The most important concept in REST is resources, which are identified by using a global ID (usually a URI). The client application uses the HTTP method (Get/post/put/delete) to manipulate the resource or resource set.
RESTful Web services are Web services that are implemented using HTTP and REST principles. In general, RESTful Web Services should define the following:
The base/root URI of the Web service, such as Http://host/<appcontext>/resources.
Supports MIME-type response data, including Json/xml/atom, and so on.
A collection of operations supported by the service (for example, POST, GET, PUT, or DELETE)
As shown in the following table:
Methods/ResourcesResource collection, URI such as:
Http://host/<appctx>/resourcesMember resource, URI such as:
http://host/<appctx>/resources/1234
GET List all members of a resource collectionRetrieves a representation of a resource identified as 1234.
PUT Updates (replaces) Another collection with one collection. Update the digital resource labeled 1234.
POST Create a digital resource in a collectionCreate a child resource below.
DELETE Deletes the entire collection of resources.Delete the digital resource labeled 1234.
II: REST and JSR (Jersey)
JSR-311 Java API for RESTful Web Services (JAX-RS) 1.0 and 1.1
Jax-rs is a new technology that will be generated in Java EE 6. Jax-rs, the Java API for RESTful Web Services, is an application interface for the Java programming language that supports the creation of WEB services in the Representational state Transfer (REST) architectural style. Jax-rs uses Java annotations introduced in Java SE5 to simplify the development and deployment of client and server side of Web services. Including:
@Path, label the relative path of the resource class or method
@GET, @PUT, @POST, @DELETE, the callout method is the type of the HTTP request.
@Produces, the type of MIME media returned by the callout
@Consumes, label the MIME media type that can accept the request
@PathParam, @QueryParam, @HeaderParam, @CookieParam, @MatrixParam, @FormParam, respectively, the parameters of the method are drawn from different locations in the HTTP request. For example, the path from the URL, @QueryParam query parameters from the URL, @HeaderParam header information from the HTTP request, @CookieParam from the cookie,@ of the HTTP request. @pathparam Formparam the form format of the post from the HTTP request.
Three: Jersey Jar Package Introduction
Jersey is a reference implementation of the Jax-rs, which contains three main parts.
Core server: By providing standardized annotations and APIs in JSR 311, you can develop RESTful Web services in an intuitive way.
Core client: The Jersey client API helps you communicate easily with REST services.
Integration (Integration): Jersey also provides libraries that can easily integrate the Spring, Guice, Apache Abdera.
Note: The jar package downloads the jersey Jar package download (requires points of the Oh, hard finishing)
Four: Create a rest resource yourself
Use the query interface of the micro account as an example
Java code
@Path ("/accinfo"
)//prgramname/rest/under the path
public class Accountinforesource {
@Context
Uriinfo Uriinfo;
@Context
Request request;
/*
* Get All accounts info
*/
@GET
@Path ("All")
///Accinfo, also the path of the outside call
@Produces (Mediatype.application_xml)
Public list<accountinfo> getallaccounts () throws unsupportedencodingexception{
list<accountinfo> retlist = new arraylist<accountinfo> ();
Entitymanager em = Entitymanagerhelper.getentitymanager ();
Maaccdtapmanager mm = new Maaccdtapmanager (EM);
list<maaccdtap> mlist = mm.getallaccounts ();
AccountInfo ai = null;
Accountadapter ad = new Accountadapter ();
for (Maaccdtap m:mlist) {
ai = Ad.getaccountinfo (m);
Retlist.add (AI);
}
Entitymanagerhelper.closeentitymanager ();
return retlist;
}
/*
*
Get Account info by mbrseq ID @GET Way
*/
@GET
@Path ("{accountid}")//
Accinfo's subpath is also the path of the outside call, both as AccountId and as a parameter
@Produces (Mediatype.application_json)
Public AccountInfo Getaccountbysid (@PathParam ("AccountId") String AccountId)
Throws unsupportedencodingexception{
Entitymanager em = Entitymanagerhelper.getentitymanager ();
Maaccdtapmanager mm = new Maaccdtapmanager (EM);
Maaccdtap MP = Mm.getaccountbysid (AccountId);
AccountInfo ai = null;
if (null! = MP) {
Accountadapter ad = new Accountadapter ();
Ai = Ad.getaccountinfo (MP);
}
Entitymanagerhelper.closeentitymanager ();
return AI;
}
/*
*
Get Account info by mbrseq ID and name @POST way
*/
@POST
@Path ("Change")
@Consumes (mediatype.application_form_urlencoded)
public void Responseaccountchange (
@FormParam (value = "id") String ID,
@FormParam (value = "name") String name,
@Context HttpServletResponse servletresponse) throws ioexception{
System.out.println ("reveiced change parameters from UI:");
SYSTEM.OUT.PRINTLN ("ID is" + ID);
System.out.println ("name is" + name);
URI Newurl = Uriinfo.getabsolutepathbuilder (). path (ID). build ();
System.out.println (Newurl.tostring ());
Response.created (Newurl). build ();
Servletoutputstream OS = Servletresponse.getoutputstream ();
PrintWriter pw = Servletresponse.getwriter ();
Pw.write ("The change request have been sent to backend and ID is" + ID);
Pw.flush ();
}
}
Test:
Use the following URL to access the appropriate account information (i.e. resource)
Http://ip:port/microacc/rest/accinfo/{mbrseq}
Http://ip:port/MicroAcc/rest/accinfo/al L
The @Produces (Mediatype.application_json) can produce the output of the JSON.
@POST Note receives an HTTP POST request that points to the address of the post in the Web form, for example:
You can receive the contents of the form by Http://ip:port/MicroAcc/rest/accinfo/change the annotated method.
V: Configuration information jersey configuration:
Jersey version 1.2 and some update maintenance versions only support Java SE 6, which you need to be aware of when choosing a version and the appropriate server. 、
The following libraries from the Jersey development package are required:
Core server: Jersey-core.jar,jersey-server.jar,jsr311-api.jar,asm.jar
Core client: (for testing) Jersey-client.jar
JAXB support: (used in advanced samples) Jaxb-impl.jar,jaxb-api.jar,activation.jar,stax-api.jar,wstx-asl.jar
JSON support: (used in advanced samples) Jersey-json.jar
(JSON is a common type of XML, the format of passing data between different projects/languages/platforms, which is more refined and better than XML, almost all languages and frameworks have been supported, and the data passed is then decoded with JSON, just like the c++struct structure. Direct json.xxx can be accessed, multi-layered words json.xxx.xxx)
You need to send all the REST requests to the Jersey container--Define the servlet scheduler in the application's Web. xml file (see Listing 1). In addition to declaring the Jersey servlet, it also defines an initialization parameter that indicates the Java package that contains the resource.
WEB.XML:XML Code
<servlet>
<servlet-name>jersey REST service</servlet-name>
<servlet-class>
Com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>sh.cmbchina.pension.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey REST service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
In this way, all resource classes under package sh.cmbchina.pension.resources are registered as response processing classes for RESTful URLs.
Introduction to Restful and Jersey (Web Service)