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

Jersey Client / JAX-RS and optional (not default) @QueryParam (client side)

我有一个 RESTful API 谁的文档说某个查询参数是可选的,并且不提供默认参数。因此,我可以提供该值,也可以不将其作为参数发送到 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);

但是当你有多个可选字段时会发生什么??我不想对它们进行所有可能的突变!

您可以将 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
  }
  ...
}

界面一直都是对的

真不敢相信这么简单:

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 注释。 要使用查询参数传递值,只需正常传值。如果您希望查询参数根本不显示,只需传递 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

哇哦!没有第二个查询参数! :)

原始值注意事项

如果您的 API 采用原始值(如 intfloatboolean 等),则使用对象包装器 class(Autoboxing) 用于该原语(如 IntegerFloatBoolean 等)。然后,您可以将 null 传递给方法:

public Response getEndpoint(@QueryParam("queryA") Boolean first);