Is JAX-RS built on top of the Servlet API? How?

I read that JAX-RS is built on top of servlets. Is this literally true, or does it just mean that it is a higher level component? If so, how does it work? Does JAX-RS create a servlet that parses the request and manually initializes the annotated @Path classes and passes the changed parameters to them? The JSR does not seem to indicate this, and not one of the books that mention it touches on any details.

note: I have no problem deploying JAX or servlets, I'm just curious to know the details, as this will provide a better understanding of how the web container works.

+5
source share
3 answers

I read that JAX-RS is built on top of servlets. This is literally true

Simply put, YES, the JAX-RS specification is built on top of Servlets, and any other deployment method (such as the one mentioned by @Jilles van Gurp ) is a specific implementation.

Does JAX-RS create a servlet that parses the request and manually initializes the annotated @Path classes and passes the changed parameters to them?

JAX-RS does nothing. This is an implementation (e.g. Jersey, RESTEasy, CXF) that implements the entry-point servlet. Is an implementation necessary for explicit query analysis? No, not all. Most of this material is handled by a servlet container. Basically, for the implementation, you just need to analyze the body of the request (since "request" means more than just the body, such as URLs, headers).

Basically, everything related to JAX-RS is handled by the implementation. The servlet container has nothing to do with passing the HttpServletRequest and HttpServletResponse, as if you were executing your own servlet. If you were to run your own JAX-RS implementation, the servlet passing you the HttpServletRequest (Response) is the request entry point, and everything else is up to you.

EDIT

as "request" means not only the body, for example URL

Bad example. In fact, the JAX-RS implementation will parse the URL to get path parameters and query parameters. Although the Servlet container will analyze the URL and add request parameters to the HttpServletRequest parameter map, this map also has the form of POST parameters, so for implementation you will also need to perform your own analysis of the request parameters.

+3
source

This is the official Jboss Resteasy documentation .

RESTeasy is implemented as a ServletContextListener and Servlet and is deployed in a WAR file.

JAX-RS implementations use ServletAPI to route and analyze requests. This is an implementation detail and need not be mentioned in the specification.

+2
source

Jax rs doesn’t really use or directly depends on servlets, but usually it is implemented on top of it using frameworks that implement it. In this case, your application terminates with a servlet that delegates incoming requests to your jax rs endpoints, and all of this is deployed in a servlet container such as tomcat or jetty.

However, for example, knitwear (reference implementation) can be performed without a servlet wrapper on a stand-alone server. For this we use grizzly. There is no servlet container in our application, and instead we use a grizzly container. Of course, the grizzly container is a very similar execution model, but you don't need a full-blown application server to run it. go here for more details on grizzly

+2
source

Source: https://habr.com/ru/post/1262081/


All Articles