Jersey Client / JAX-RS和可选(非默认)@QueryParam(客户端) [英] Jersey Client / JAX-RS and optional (not default) @QueryParam (client side)

查看:110
本文介绍了Jersey Client / JAX-RS和可选(非默认)@QueryParam(客户端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a RESTful API who's document says that a certain query parameter is optional, and does not supply a default argument. So, I can either supply the value or not send it in the GET request as a parameter.

示例:


  • queryA 是必需的

  • queryB 可选(可以发送 GET 没有它)

  • queryA is required
  • queryB is optional (can send GET without it)

这应该有效:

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-用于生成客户端以与服务器API交互的代理。

How do I make an client interface for Jersey-Proxy that can do this?? I do not have the server-side code to interface with so I am using org.glassfish.jersey.client.proxy.WebResourceFactory via Jersey-Proxy to generate the client to interact with the server 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);

}

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

I know I can make another method:

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

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

But what happens when you have multiple optional fields?? I don't want to make every possible mutation of them!

推荐答案

界面一直都是正确的



我不敢相信这很容易:

The interface was right all along

I can't believe it was this easy:

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);

}

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

Notice anything different than the questions interface?? Nope. That's because that is the answer!

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

If you want to default a parameter to a specific value, you use the @DefaultValue annotation in the parameter:

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


Pass null to the @QueryParam you don't want

If you want to make the @QueryParam optional, you do not apply the @DefaultValue annotation. To pass a value with the query parameter, just pass in the value normally. If you would like the query parameter to not show up at all, just pass 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

还有walla!没有第二个查询参数! :)

And walla! No second query parameter! :)

这篇关于Jersey Client / JAX-RS和可选(非默认)@QueryParam(客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆