More Than Five
More Than Five

Reputation: 10419

Get the original URI in ExceptionMapper

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

Upvotes: 2

Views: 836

Answers (2)

cassiomolin
cassiomolin

Reputation: 130917

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.

Upvotes: 2

Ori Marko
Ori Marko

Reputation: 58772

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();

Upvotes: 1

Related Questions