简体   繁体   中英

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

I've been reading that the JAX-RS is built on top of servlets. Is this literally true, or it just mean that it is a higher level component? If it is, how does that work? Does JAX-RS create a servlet which parses the request and manually initializes @Path annotated classes and passes the modified parameters to them? The JSR does not seem to specify this, and none of the books that mention it go into any details.

note: I don't have trouble deploying JAX or servlets, I am just curious about the details, as it would provide a better understanding of how the web container works.

I've been reading that the JAX-RS is built on top of servlets. Is this literally true,

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

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

JAX-RS doesn't do anything. It's the implementation (eg Jersey, RESTEasy, CXF) that implements the entry point servlet. Does the implementation need to explicitly parse the request? No, not all of it. Most of that stuff is handled by the servlet container. Mainly the implementation will just need to parse the request body (as "request" implies more than just the body, eg URL, headers).

Basically, everything related to JAX-RS is handled by the implementation. The servlet container has nothing to with anything but passing the HttpServletRequest and HttpServletResponse, just like if you were to implement your own servlet. If you were to make your own JAX-RS implementation, the servlet passing you the HttpServletRequest(Response) is the request entry point, and everything else is up you.

EDIT

as "request" implies more than just the body, eg URL

Bad example. Actually, the JAX-RS implementation would parse the URL in order to get path parameters and query parameters. Though the Servlet container will parse the URL and add query parameters to the HttpServletRequest parameters map, that map also has form POST parameters, so the implementation will need to do it's own parsing of the query parameters also.

This is the official documentation of Jboss Resteasy.

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

JAX-RS implementations do use ServletAPI for routing and parsing the requests. It is implementation detail and need not be mentioned in the specification.

Jax rs does not really use or depend on servlets directly but it is commonly implemented on top of it by frameworks that implement it. In that case your application is wrapped with a servlet that delegates incoming requests to your jax rs endpoints and the whole thing is deployed in a servlet container such as tomcat or jetty.

However, for example jersey (the reference implementation) can run without a servlet wrapper in a standalone server. We use grizzly as our container for this. There is no servlet container in our application and we use the grizzly container instead. Of course the grizzly container provides 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM