如何从 JAX-RS 获取原始请求的 URI javax.ws.rs.core.Response

How to get the original requested URI from JAX-RS javax.ws.rs.core.Response

我正在尝试为 JAX-RS 编写自定义 ExceptionMapper class。我想以某种方式从 JAX-RS javax.ws.rs.core.Response 对象读取原始请求的 URI。

当我在调试模式下使用 IntelliJ IDEA 检查响应对象时,我可以在以下路径下看到此信息:response > context > resolvedUri where
context 的类型是 org.glassfish.jersy.client.ClientResponse。这个 class 有一个 resolvedUri 变量,它保存我需要的信息。

我能以某种方式获得此信息吗?如何编写我的 getRequestUri(response) 方法?

public class MyExceptionMapper implements ExceptionMapper<WebApplicationException> {

    @Override
    public Response toResponse(WebApplicationException error) {

        Response response = error.getResponse();
        ErrorResponse errorResponse = ErrorResponseBuilder
                .builder()
                .httpStatus(getDefaultStatusCodeIfNull(response))
                .errorMessage(getCustomErrorMessage(response))
                .requestedUri(getRequestedUri(response)) <--------- HOW TO READ IT?
                .build();

        return Response
                .status(errorResponse.getHttpStatus())
                .type(ExtendedMediaType.APPLICATION_JSON)
                .entity(errorResponse)
                .build();
    }
}

使用

@Context private HttpServletRequest servletRequest;

并使用HttpServletRequest.getRequestURI()

public class MyExceptionMapper implements 
     ExceptionMapper<WebApplicationException> {

    @Context
    private HttpServletRequest servletRequest;

    @Override
    public Response toResponse(WebApplicationException error) {

        Response response = error.getResponse();
        ErrorResponse errorResponse = ErrorResponseBuilder
                .builder()
                .httpStatus(getDefaultStatusCodeIfNull(response))
                .errorMessage(getCustomErrorMessage(response))
                .requestedUri(servletRequest.getRequestURI())
                .build();

        return Response
               .status(errorResponse.getHttpStatus())
               .type(ExtendedMediaType.APPLICATION_JSON)
               .entity(errorResponse)
               .build();
    }
}