gpt4 book ai didi

rest - 使用 PreMatching 过滤器更改 uri 路径

转载 作者:行者123 更新时间:2023-12-01 00:46:32 25 4
gpt4 key购买 nike

简而言之,我在 jar 中有一个 Jersey REST 服务,我需要使用与服务注释中定义的映射不同的映射将其部署在我的 web 应用程序中。在服务中,我有@ApplicationPath("/rest")@Path("/foo") .但是,传入的请求将采用以下形式:http://example.com/delegate/rest/foo (注意,delegate 不是上下文路径,而是映射到 ROOT webapp 中的 servlet,它加载 session 信息并将请求代理到我的 webapp,这意味着我不能像通常那样用 @ApplicationPath 覆盖 servlet-mapping案件)。所以,我想做的是:

@PreMatching
@Priority( 500 )
public class DelegateRemappingFilter implements ContainerRequestFilter {
private static final Logger LOGGER = LoggerFactory.getLogger( DelegateRemappingFilter.class );

@Override
public void filter( ContainerRequestContext requestContext ) throws IOException {
UriInfo uriInfo = requestContext.getUriInfo();

// convert baseUri to http://example.com/delegate/rest
URI baseUri = uriInfo.getBaseUriBuilder()
.path( uriInfo.getPathSegments().get( 0 ).getPath() ).build();
URI requestUri = uriInfo.getRequestUri();

// As expected, this will print out
// setRequestUri("http://example.com/delegate/rest","http://example.com/delegate/rest/foo")
LOGGER.debug( "setRequestUri(\"{}\",\"{}\")", baseUri, requestUri );
requestContext.setRequestUri( baseUri, requestUri );
}
}

然而,这最终未能匹配。是否无法修改 @PreMatching 中 URI 的路径部分?筛选?我认为这就是这种类型的过滤器的用途......

最佳答案

我讨厌在发布后几分钟找到自己的答案...无论如何,baseUri必须以 / 结尾.所以改变这个:

    URI baseUri = uriInfo.getBaseUriBuilder()
.path( uriInfo.getPathSegments().get( 0 ).getPath() ).build();

对此:
    URI baseUri = uriInfo.getBaseUriBuilder()
.path( uriInfo.getPathSegments().get( 0 ).getPath() + "/" ).build();

成功了。

关于rest - 使用 PreMatching 过滤器更改 uri 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34090950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com