Java rest framework with programmable dispatcher? - java

I'm searching a java rest framework or JAX-RS implementation that allows me to configure the dispatcher at runtime through an API. In JAX-RS it is only possible to do the configuration with #Path and #HttpMethod annotations or by effectively building your own dispatcher in a sub-resource.
I'm searching for:
dispatcher.addResource("/my/{path}", Resource.class) or
dispatcher.addResource("/2nd/path", resourceHandlerFactory)

Unfortunately there is nothing in JAX-RS for dynamic resource specification as you've found. Apache Wink has another alternative to Restlet called DynamicResource http://incubator.apache.org/wink/1.1/html/5.1%20Registration%20and%20Configuration.html. I'd be quite interested in why you need dynamic resources as I had similar requirement.

Try Restlet. It provides runtime dispatch functionality through its Router class. See http://wiki.restlet.org/docs_2.1/13-restlet/27-restlet/326-restlet.html for an example.

Related

Spring-MVC: Programmatic API for Building REST Resources like in Jersey?

The standard way for creating REST resources both in JAX-RS and Spring MVC is to use annotations. Jersey provides an alternative to using annotations, which is called Programmatic API for Building Resources. This can be used to create REST resources dynamically at runtime, for example from user input or configuration. With the annotations approach, in contrast, creating a new REST resource requires the usual develop/build/deploy cycle.
Is there an equivalent API for building REST resources programmatically in Spring MVC? I'm looking for working example code like in the Programmatic Hello World example of the Jersey user guide.

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 (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.
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
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.

When to use RequestContextFilter with Jersey?

In a bunch of the tutorials and code samples of Spring Boot and Jersey that I've seen, the following line appears:
register(RequestContextFilter.class)
What is this really used for? I don't see anything unusual in those samples, and if I remove it from my (simple) application, nothing seems to break.
RequestContextFilter's javadoc says
This filter is mainly for use with third-party servlets, e.g. the JSF
FacesServlet. Within Spring's own web support, DispatcherServlet's
processing is perfectly sufficient.
I haven't seen third party servlets in those examples.
In one of them I read
org.glassfish.jersey.server.spring.scope.RequestContextFilter, which
is a Spring filter that provides a bridge between JAX-RS and Spring
request attributes
What would be an example of a Spring request attribute?
What is some typical use case, besides needing a third party servlet?

Restlet Client using JAX-RS annotated resources

I am writing a restlet client that will invoke some Resteasy coded rest services (cannot change the server code, hence cannot use the Restlet way of annotating resources).
Resource Interface is using JAX-RS annotations and have more than one #POST method (one of the biggest problems of Restlet when dealing with this).
I was trying to do my implementaion this way:
IAppLoginResource resource = JaxRsClientResource.createJaxRsClient("http://localhost:9090/rest", IAppLoginResource.class);
final GetLoginAppInfoResponse response = resource.getLoginAppInfo( getLoginAppInfoRequest );
The problem is that the request by default is GET, I didn't find a way to specify the request method like when using ClientResource (which I can't use because I need to deal with JaxbRepresentation and Jaxb problems).
Any sample/snippet of code that implement a Restlet client using JAX-RS annotated resources?
Any ideas?
Thanks,
I've entered an issue for this topic:
https://github.com/restlet/restlet-framework-java/issues/1081
I've tested a sample application based on your code, and it works properly using the current 2.3 branch (future 2.3.3). I wonder if the fix for this issue https://github.com/restlet/restlet-framework-java/issues/1072 helps.
Regarding the documentation, I 'll complete the current page (http://restlet.com/technical-resources/restlet-framework/guide/2.3/extensions/jaxrs), cf this issue: https://github.com/restlet/restlet-framework-java/issues/1084.
You can also have a look at the org.restlet.test project, especially in this package https://github.com/restlet/restlet-framework-java/tree/2.3/modules/org.restlet.test/src/org/restlet/test/ext/jaxrs.

Request handling methods in spring

Which method is better to used to handle the request whether to use requestHandle of AbstractController or formBackingObject of AbstractFormController in the spring framework?
Not much information given...
If you are a beginner and not tied to any spcific version of Spring I'd suggest looking at the spring 3.0 MVC implementation.
It is annotation driven so you can define your own methods to use.
See here for docs:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-features

Categories

Resources