2

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,959
  • 21
  • 77
  • 127

2 Answers2

2

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
  • 124,154
  • 35
  • 280
  • 359
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();
Ori Marko
  • 56,308
  • 23
  • 131
  • 233