【问题标题】:resteasy, how to initialize @Context UriInfo parameter for use in proxy client call?resteasy,如何初始化@Context UriInfo 参数以在代理客户端调用中使用?
【发布时间】:2017-03-07 14:58:43
【问题描述】:

我的 resteasy 端点接口声明如下:

@Path("/entity")
public interface EntitySearchEndpoint {
...
    @GET
    @Path("/search")
    @Produces(MediaType.APPLICATION_JSON)
    public Response search(@Context UriInfo ui);
...
}

现在我使用接口创建了代理客户端

ResteasyClient reClient = new ResteasyClientBuilder().build();
ResteasyWebTarget webTarget = reClient.target(URI.create("http://example.com"));
EntitySearchEndpoint entitySearchEndpoint = ncspAPIWebTarget.proxy(EntitySearchEndpoint.class);

现在我可以使用

调用服务了
UriInfo ui = ???
Response response = entitySearchEndpoint.search(ui);

我的问题是如何创建 UriInfo 实例以仅包含所需的查询参数?
可能使用@Context UriInfo作为参数不正确,正确的方法是什么?
QueryParam 名称列表不受限制,允许任何名称...

【问题讨论】:

    标签: jakarta-ee jax-rs resteasy


    【解决方案1】:

    如果我正确理解了您的需求,您的 API 可能如下所示:

    @Path("/entity")
    public interface EntitySearchEndpoint {
    ...
        @GET
        @Path("/search/{searchQuery}")
        @Produces(MediaType.APPLICATION_JSON)
        public Response search(@PathParam("searchQuery") String searchQuery);
    ...
    }
    

    你的客户只会用一个字符串来调用它:

    Response response = entitySearchEndpoint.search("test string");
    

    【讨论】:

    • 谢谢兰迪,我终于找到解决方案了,我什至不记得了,但我稍后会在这里写。关键是搜索参数在查询参数中,意思是问号 '?paam1=value1&param2=value2&.....'
    • 我明白了,那么您可能正在使用@QueryParam。我不会使用 UriInfo (您不会将其作为这样的参数传递),因为查看您的 api 代码的人不会意识到它有输入。 QueryParam 更加清晰。
    • 是的,但参数的数量可能会有所不同,其名称也可能会有所不同......
    猜你喜欢
    • 2017-04-08
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多