There is a service that has an expanded URL-tree. The whole service works on JAX-RS (more precisely RESTeasy under WildFly). Everything works fine, but recently a problem has arisen: is it possible in any way to handle requests that have gone beyond the tree? That is, for example, there are several described URL chains like:

/users/

/users/{id}/

/users/{id}/connect

And so on. At the same time, there are no subnets in users except {id}. Suppose that suddenly a request arrives at the service along the path: /users/smth/ , that is, the path that is not /users/smth/ in the service. And this is where the problem begins, the server returns exception and returns an empty response. Is it possible to somehow customize the processing of such undiscovered ways?

    1 answer 1

    Usually, ExceptionMapper is written and registered for a specific type of exception. In the case of curved paths, this is likely to be a NotFoundException .

    So we can write something like this:

     @Provider public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> { Response toResponse(NotFoundException exception) { // реализуем тут логику } } 

    The @Provider allows RESTEasy to independently find your class (like @Component in Spring).