REST (1) REST and JAX-RS

Recently, I reorganized the code and recorded several frameworks and methods for implementing REST api in java.

1. What is REST
Representational state transfer (REST) ​​Web Service:
It is a software architecture style proposed by Dr. Roy Fielding in his doctoral thesis in 2000. In the current three mainstream Web Service implementation schemes, because REST is more concise than SOAP and XML-RPC, more and more Web Services begin to be designed and implemented in REST style.
REST describes an architectural style of interconnected systems (such as web applications). REST constraints, when applied as a whole, result in a simple, scalable, efficient, secure, and reliable architecture. RESTful Web services are one of the most promising alternatives to SOAP-based services due to their simplicity, light weight, and direct data transfer over HTTP. A multi-tier architecture for web services and dynamic web applications enables a clean separation of reusability, simplicity, extensibility, and component responsiveness. Developers can easily create rich interfaces using Ajax and RESTful Web services together.
The key to RESTful is to define objects that can represent process elements/resources. In REST, each object is represented by a URL, and the object user is responsible for packing state information into each message so that the processing of objects is always stateless.

Some sources about REST are as follows:
What does RESTful really mean?

2. JAX-RS
JAX-RS is a new technology introduced by JAVA EE6 (JSR311: JAX-RS: The Java API for RESTful Web Services). JAX-RS, the Java API for RESTful Web Services, is an application programming interface in the Java programming language that supports the creation of Web services according to the Representational State Transfer (REST) ​​architectural style. JAX-RS uses the Java annotations introduced in Java SE5 to simplify the development and deployment of web services clients and servers.
Annotations include:
@Path, which annotates the relative path of the resource class or method
@GET, @PUT, @POST, @DELETE, and the annotation method is the type of the HTTP request.
@Produces, mark the returned MIME media type
@Consumes, mark the MIME media type that can accept the request
@PathParam, @QueryParam, @HeaderParam, @CookieParam, @MatrixParam, @FormParam, respectively mark the parameters of the method from different locations of the HTTP request For example, @PathParam comes from the path of the URL, @QueryParam comes from the query parameters of the URL, @HeaderParam comes from the header information of the HTTP request, and @CookieParam comes from the cookie of the HTTP request.
The REST framework developed based on java basically follows the JAX-RS specification and supports the REST declaration in the form of annotations.

3. API design
Restful-style interface abstracts functions into resource resource path mapping, which is accessed in the form of HTTP GET /resource/{id}. Mainly divided into the following types of interfaces:
address request method illustrate
/resources GET Get all resources
/resources POST Create a new resource, the content contains the resource content
/resource/{id} GET Get the resource with the id number
/resource/{id} PUT Update the resource numbered id, the content contains the resource content
/resource/{id} DELETE delete resource with id

{id} is called a path variable, which tells restful which resource you want to check, modify, and delete.
The interface generally returns data in json/xml format, which is convenient for server programs and browser scripts to call the interface and process the returned data.
There are many details that need to be paid attention to in the specific design. You can refer to the following materials:
RESTful API Design Guidelines
RESTful API Design Best Practices

4. Implementation Framework Common open source REST frameworks
based on java are as follows:
  • Spring MVC
  • Jersey
  • RESTEasy
  • Restlet
  • Apache CXF

See Spring REST WS: jersey vs resteasy vs restlet vs apache cxf vs Spring WS
Each framework supports JAX-RS annotations. The various framework implementation details are described in sections later.

5. Spring MVC implements REST
For an example of implementing REST based on spring4+springmvc, see springmvc series spring mvc (6) Implementing REST interface GET/POST/PUT/DELETE

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803065&siteId=291194637