Rest architecture and RESTful Web Service

Rest architecture 101

In the micro-services popular today, Rest architecture in which the role can not be ignored. This article from the following four questions starting, and gradually establish the overall concept of the Rest of architecture and RESTful Web Service.

  • What is the Rest?
  • What is RESTful Web Service?
  • What is JAX-RS?
  • JAX-RS and Spring MVC differences and relations?
  • to sum up

Rest--REpresentational State Transfer

Rest is using HTTP protocol, designed based on existing web standards application architecture. It is the first time in 2000 presented by Roy Fielding.

Rest of the content management application abstracted as resources that can be text files, web pages, images, videos and so on. GET clients via HTTP protocol definition, POST, DELETE, PUT and other standard methods of access to resources.

REST support a variety of formats for describing resources, more common, such as Text, JSON, XML.

HTTP method Introduction:

  1. GET - Query Resources
  2. POST - Create a resource
  3. DELETE - delete the resource
  4. PUT - update exists, otherwise create a resource

RESTful Web Service

Simple to understand Web Service is a contract, which consists of a series of protocols and standards. Object is defined between applications or systems should be performed in what form data exchange. Even though the application running different platforms (operating systems, cloud platform), different programming languages, between applications can exchange data over a computer network using Web Service.

RESTful Web Service method to achieve that is to use HTTP REST architecture of Web Service.

A RESTful web service typically define a URI - that uniquely identifies the resources, resource description formats such as JSON, a list of resources to support HTTP methods GET, POST and so on.

RESTful web service instance:

HATE HTTP Method POST BODY Result
/UserService/listUsers GET emtpy List of all users.
/UserService/addUser POST JSON Detail of new user.
/UserService/getUser/:id GET empty Detail of user.

JAX-RS -- JAVA API for RESTful Web Services

JAX-RSR is an extension of the Servlet API, to support RESTful Web Services and the development of interface standards and standard JAX-RSR 2.1-370 that is JSR .

2.0 version was released in 2013, from the beginning of Java SE 5 supports the creation of Web Service annotations by the way. JAX-RSR reference implementation: Jersey-- the Oracle implemented by the servlet , RESTEasy - Red Hat , the Apache CXF

Spring Framework vs JAX-RS

Spring occur because earlier than JAX-RSR, so it does not implement this standard. The Spring MVC can be seen as an alternative to the JAX-RSR.

Examples

** follow Resource Controller JAX-RS standard API: **

@Path("/greetings")
public class JaxRsController {

    @GET
    @Path("/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response greeting(@PathParam("name") String name) {
     String greeting = "Hello " + name;
     return Response.ok(greeting).build();
    }
}
复制代码

** Follow equivalent Spring MVC API implementation: **

@RestController
@RequestMapping("/greetings")
public class SpringRestController {
    @RequestMapping(method = RequestMethod.GET, value = "/{name}", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<?> greeting(@PathVariable String name) {
       String greeting = "Hello " + name;
       return new ResponseEntity<>(greeting, HttpStatus.OK);
    }
}

复制代码

to sum up

RESTful is an application architecture, RESTful Web Service was RESTful HTTP protocol architecture is based on standard methods of implementation.

JAX-RSR is a standard API for JAVA language developed, which supports RESTful Web Service architecture and specification.

Java ecosystem of manufacturers to provide different implementations of JAX-RSR in line with standards and combine their own Servlet container such as Jersey - Oracle, RESTEasy - Red Hat, Apache Cxf - Apache Org and so on.

As early as the Spring Framework to develop JAX-RSR standard, which is not native to achieve this specification. The Spring MVC module offers another Spring programming model to implement RESTful Web Service architecture.

Reproduced in: https: //juejin.im/post/5cfcda2fe51d4510664d16a1

Guess you like

Origin blog.csdn.net/weixin_34198797/article/details/91429070