1

I am using an ExceptionMapper in JAX-RS.

 public class MyException implements ExceptionMapper<ConstraintViolationException> {

    @Override
    public Response toResponse(ConstraintViolationException exception) {
        ...
    }
}

all works. However, is there anyway I can get a handle to the URL, or the HTTP request that generated the exception?

Thanks

More Than Five
  • 9,311
  • 20
  • 72
  • 121

2 Answers2

1

Use @Context in class and use it to get original request:

@Context
private HttpServletRequest request;
@Override
public Response toResponse(ConstraintViolationException exception) {
   String requestURI = request.getRequestURI();
user7294900
  • 52,490
  • 20
  • 92
  • 189
1

Inject UriInfo using @Context:

@Context
private UriInfo uriInfo;

Then use the getRequestUri() method to get request URI:

URI uri = uriInfo.getRequestUri();

Refer to this answer for other types that can be injected with @Context.

cassiomolin
  • 113,708
  • 29
  • 249
  • 319