简体   繁体   English

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

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

I am trying to write a custom ExceptionMapper class for JAX-RS. 我正在尝试为JAX-RS写一个自定义的ExceptionMapper类。 I would like to read somehow the original requested URI from the JAX-RS javax.ws.rs.core.Response object. 我想以某种方式从JAX-RS javax.ws.rs.core.Response对象读取原始请求的URI。

When I check the response object with IntelliJ IDEA in debug mode then I can see this info under the following path: response > context > resolvedUri where 当我在调试模式下使用IntelliJ IDEA检查响应对象时,可以在以下路径下看到此信息:response> context> resolveUri其中
type of the context is org.glassfish.jersy.client.ClientResponse . context类型是org.glassfish.jersy.client.ClientResponse This class has a resolvedUri variable which holds the info I need. 此类具有resolvedUri变量,其中包含我需要的信息。

Can I get this info somehow? 我能以某种方式获得此信息吗? How I can write my getRequestUri(response) method? 如何编写我的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();
    }
}

Use 采用

@Context private HttpServletRequest servletRequest; @Context私有HttpServletRequest servletRequest;

And use HttpServletRequest.getRequestURI() 并使用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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM