Can I wrap all JAX-RS requests with custom pre-dispatch, post-dispatch and error-handler code?

You can also use ExceptionMappers. This mechanism which catch the exception thrown by your service and convert it to the appropriate Response: @Provider public class PersistenceMapper implements ExceptionMapper { @Override public Response toResponse(PersistenceException arg0) { if(arg0.getCause() instanceof InvalidDataException) { return Response. Status(Response.Status.

BAD_REQUEST).build(); } else { ... } } } For more information see: stackoverflow.com/questions/3293599/jax-....

You can use filters to read and modify all requests and responses.

Looks like filters can only be used by clients! – fjsj Nov 5 '10 at 18:42.

You could create a proxy RESTful service and use this as the entry point to all your other RESTful services. This proxy can receive requests, do any pre-processing, call the RESTful service required, process the response and then return something to the caller. I have a set up like this in a project I've been working on.

The proxy performs functions like authentication, authorisation and audit logging. I can go into further details if you like. Edit: Here is an idea of how you might want to implement a proxy that supports GET requests; @Path("/proxy") public class Proxy { private Logger log = Logger.

GetLogger(Proxy. Class); @Context private UriInfo uriInfo; @GET @Path("/{webService}/{method}") public Response doProxy(@Context HttpServletRequest req, @PathParam("webService") String webService, @PathParam("method") String method) { log. Debug("log request details"); //implement this method to work out the URL of your end service String url = constructURL(req, uriInfo, webService, method); //Do any actions here before calling the end service Client client = Client.create(); WebResource resource = client.

Resource(url); try { ClientResponse response = resource. Get(ClientResponse. Class); int status = response.getStatus(); String responseData = response.

GetEntity(String. Class); log. Debug("log response details"); //Do any actions here after getting the response from the end service, //but before you send the response back to the caller.

Return Response. Status(status). Entity(responseData).build(); } catch (Throwable t) { //Global exception handler here //remember to return a Response of some kind.

} }.

You can also use ExceptionMappers. This mechanism which catch the exception thrown by your service and convert it to the appropriate Response.

Up vote 5 down vote favorite 2 share g+ share fb share tw.

I have a number of classes exposed as JAX-RS request "handlers", using javax.ws.rs. Path annotations. I want to add certain actions before every request and after each request.

Also, I need to create a global application-wide exception handler, which will catch everything thrown by these handlers and protocol. Is it possible to achieve this with standard JAX-RS without creating of a custom class inherited from com.sun.jersey.spi.container.servlet. ServletContainer (I'm using Jersey).

Java jersey jax-rs link|improve this question asked Oct 12 '10 at 15:08yegor2565,4951562 81% accept rate.

The accepted answer doesn't address your desire to have pre and post request actions. Did you ever come to a solution for that? – A.

R. Younce May 7 '11 at 0:26 Custom Servlet Filter is the way to go. – yegor256 May 7 '11 at 3:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions