Jersey Client / JAX-RS和可选(非默认)@QueryParam(客户端)

我有一个RESTful API,他的文档说某个查询参数是可选的,并且不提供默认参数。 因此,我可以提供值,也可以不在GET请求中将其作为参数发送。

例:

  • queryA是必需的
  • queryB可选的 (可以在没有它的情况下发送GET

这应该工作:

 http://www.example.com/service/endpoint?queryA=foo&queryB=bar 

这应该也有效:

 http://www.example.com/service/endpoint?queryA=foo 

如何为Jersey-Proxy创建一个可以执行此操作的客户端界面? 我没有与服务器端代码进行交互,因此我使用org.glassfish.jersey.client.proxy.WebResourceFactory通过Jersey-Proxy生成客户端以与服务器API进行交互。

样本界面:

 import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/service") @Produces("application/json") public interface ServiceInterface { @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first, @QueryParam("queryB") String second); } 

我知道我可以制作另一种方法:

  @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first); 

但是当你有多个可选字段时会发生什么? 我不想让它们发生任何可能的变异!

界面一直都是正确的

我简直不敢相信这很容易:

 import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/service") @Produces("application/json") public interface ServiceInterface { @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first, @QueryParam("queryB") String second); } 

注意与问题界面有什么不同? 不。 那是因为这就是答案!


不要使用@DefaultValue

如果要将参数默认为特定值,请在参数中使用@DefaultValue批注:

 import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/service") @Produces("application/json") public interface ServiceInterface { @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first, @QueryParam("queryB") @DefaultValue("default") String second); } 

null传递给您不想要的@QueryParam

如果要使@QueryParam可选,则不应用@DefaultValue批注。 要使用query参数传递值,只需正常传入值即可。 如果您希望查询参数根本不显示,只需传递null

 import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/service") @Produces("application/json") public interface ServiceInterface { @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first, // Pass null to this parameter to not put it in the GET request @QueryParam("queryB") String second); } 

所以调用ServiceInterface.getEndpoint("firstQueryParam", "secondQueryParam"); 要求:

 http://targethost.com/service/endpoint?queryA=firstQueryParam&queryB=secondQueryParam 

并调用ServiceInterface.getEndpoint("firstQueryParam", null); 要求:

 http://targethost.com/service/endpoint?queryA=firstQueryParam 

还有沃拉! 没有第二个查询参数! 🙂

您可以将UriInfo实例(或其他类似HttpServletRequest )注入到您的方法中,并获取您想要的任何数据。

例如

 @Path("/endpoint") @GET public Response getEndpoint(@Context UriInfo info, @QueryParam("queryA") String queryA) { String queryB = info.getQueryParameters().getFirst("queryB"); if (null != queryB) { // do something with it } ... }