UEx-specific ExceptionMapper in JAX-RS

I use Jersey and Guice is my IOC container. I would like to know if ExceptionMapper can be associated with a specific URI. The reason for this is because I want to map the same exception differently based on which URI was visited. For example, suppose I have the following two exceptions for my custom exception:

public class MyExceptionMapperForFirstURI implements ExceptionMapper<MyException> {..return response based on first URI..} public class MyExceptionMapperForSecondURI implements ExceptionMapper<MyException> {..return response based on second URI..} 

As far as I understand, you bind ExceptionMapper in your ServletModule as follows:

 public class MyModule extends ServletModule { @Override public void configureServlets() { super.configureServlets(); bind(MyCustomExceptionMapper.class); } } 

How would I contact the MyExceptionMapperForFirstURI and MyExceptionMapperForSecondURI so that they are associated with the correct URIs. Is it possible, and if it is possible: is it right to do it?

+4
source share
2 answers

This is a rather late answer ;-), but you can always enter URIInfo and the branch. In this way,

 @Context UriInfo uriInfo; 

.....

 if (matchesA(uriInfo.getAbsolutePath())) { // do something } 
+3
source

You do not know what the URI of your application looks like, but if you can split your application into two servlets or filters, then you can do it like this: for example, one servlet / filter serves one set of resources and includes the first resolver and the other servlet / filter serves another a set of resources and include another mapper.

If these are custom exceptions, you can also pass Request as an argument for the exception and have only one resolver - decide on the answer based on the uri request in mapper.

+3
source

All Articles