Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 785
blue_note Avatar asked Dec 30 '16 13:12

blue_note


People also ask

Which of the following is the implementation of the JAX-RS API?

JAX-RS is a standard defined in Java Specification Request 311 (JSR-311) and Jersey / RESTEasy are implementations of it.

Which annotation is used for creating a RESTful web service using JAX-RS?

Developing RESTful Web Services with JAX-RS. JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture. The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.

Is JAX-RS a framework?

JAX-RS is only a specification and it needs a compatible implementation to be used. On the other hand, Spring MVC is a complete framework with REST capabilities. Like JAX-RS, it also provides us with useful annotations to abstract from low-level details.


1 Answers

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 (e.g. 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, e.g 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, e.g 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.

like image 79
Paul Samsotha Avatar answered Sep 17 '22 15:09

Paul Samsotha