Jakarta ee resteasy,如何初始化@Context UriInfo参数以用于代理客户端调用?
我的resteasy端点接口声明如下:Jakarta ee resteasy,如何初始化@Context UriInfo参数以用于代理客户端调用?,jakarta-ee,jax-rs,resteasy,Jakarta Ee,Jax Rs,Resteasy,我的resteasy端点接口声明如下: @Path("/entity") public interface EntitySearchEndpoint { ... @GET @Path("/search") @Produces(MediaType.APPLICATION_JSON) public Response search(@Context UriInfo ui); ... } @Path("/entity") public interface EntityS
@Path("/entity")
public interface EntitySearchEndpoint {
...
@GET
@Path("/search")
@Produces(MediaType.APPLICATION_JSON)
public Response search(@Context UriInfo ui);
...
}
@Path("/entity")
public interface EntitySearchEndpoint {
...
@GET
@Path("/search/{searchQuery}")
@Produces(MediaType.APPLICATION_JSON)
public Response search(@PathParam("searchQuery") String searchQuery);
...
}
现在,我使用该接口创建了代理客户端
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名称列表不受限制,允许使用任何名称…如果我正确理解了您想要的内容,您的API可能如下所示:
@Path("/entity")
public interface EntitySearchEndpoint {
...
@GET
@Path("/search")
@Produces(MediaType.APPLICATION_JSON)
public Response search(@Context UriInfo ui);
...
}
@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");
如果我正确理解了您的需求,您的API可能会如下所示:
@Path("/entity")
public interface EntitySearchEndpoint {
...
@GET
@Path("/search")
@Produces(MediaType.APPLICATION_JSON)
public Response search(@Context UriInfo ui);
...
}
@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¶m2=value2&…”之后,我明白了,那么您可能使用了@QueryParam。我不会使用UriInfo(您不会将其作为那样的参数传递),因为查看您的api代码的人不会意识到它有输入。QueryParam更清晰。是的,但参数的数量可能会有所不同,其名称也可能有所不同……谢谢Randy,我终于找到了解决方案,现在我甚至都不记得了,但是,我稍后会尝试在这里编写它。关键是搜索参数在查询参数中,意思是在问号“?paam1=value1¶m2=value2&…”之后,我明白了,那么您可能使用了@QueryParam。我不会使用UriInfo(您不会将其作为那样的参数传递),因为查看您的api代码的人不会意识到它有输入。QueryParam更清晰。是的,但参数的数量可能会有所不同,其名称也可能有所不同。。。