代码之家  ›  专栏  ›  技术社区  ›  Nyamiou The Galeanthrope

使用CXF代理客户端时在运行时添加查询参数

  •  0
  • Nyamiou The Galeanthrope  · 技术社区  · 6 年前

    所以我使用CXF-RS代理特性来创建可重用的REST客户机,我将在多个应用程序中使用它。所以我有一个接口,类似于:

    @Path("/hello")
    public interface HelloService {
    
        @GET
        @Path("sayHello")
        String sayHello(@QueryParam("name") String name);
    }
    

    我正在创建客户机:

    JAXRSClientFactory.create(address, HelloService.class, Collections.singletonList(JacksonJsonProvider.class), true)
    

    但是现在我需要根据应用程序的配置向请求发送一个额外的查询参数。我不想更改接口HelloService,而是使用某种过滤器来处理这个问题。我看到了 ClientRequestFilter 但是我不知道它是否是正确的工具,以及我应该如何将它添加到代理中(我看到的所有教程都在使用它) ClientBuilder.newClient() 而不是代理人)。

    先谢谢你。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Paul Samsotha    6 年前

    ClientRequestFilter 为了这个。假设您想添加一个查询参数。你可以这样做

    public class MyClientFilter implements ClientRequestFilter {
    
        @Override
        public void filter(ClientRequestContext request) throws IOException {
            request.setUri(UriBuilder.fromUri(request.getUri())
                    .queryParam("foo", "bar")
                    .build());
        }
    }
    

    要注册它,只需将它添加到作为第三个参数传递给的列表中 JAXRSClientFactory.create . 看一下文件 JAXRSClientFactory create 接受提供程序列表的方法。这个 是一种提供者。

    推荐文章