jus*_*erb 17 java rest jax-rs jersey
我有一个RESTful API,他的文档说某个查询参数是可选的,并且不提供默认参数.因此,我可以提供值,也可以不在GET请求中将其作为参数发送.
例:
queryA 是必须的queryB是可选的(GET没有它可以发送)这应该工作:
http://www.example.com/service/endpoint?queryA=foo&queryB=bar
Run Code Online (Sandbox Code Playgroud)
这应该也有效:
http://www.example.com/service/endpoint?queryA=foo
Run Code Online (Sandbox Code Playgroud)
如何为Jersey-Proxy创建一个可以执行此操作的客户端界面?我没有与服务器端代码进行交互,因此我使用org.glassfish.jersey.client.proxy.WebResourceFactoryJersey-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);
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以制作另一种方法:
@Path("/endpoint")
@GET
public Response getEndpoint(
@QueryParam("queryA") String first);
Run Code Online (Sandbox Code Playgroud)
但是当你有多个可选字段时会发生什么?我不想让它们发生任何可能的变异!
jus*_*erb 29
我简直不敢相信这很容易:
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);
}
Run Code Online (Sandbox Code Playgroud)
注意什么不同于问题界面?不.那是因为这就是答案!
如果要将参数默认为特定值,请使用@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);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
所以打电话ServiceInterface.getEndpoint("firstQueryParam", "secondQueryParam");:
http://targethost.com/service/endpoint?queryA=firstQueryParam&queryB=secondQueryParam
Run Code Online (Sandbox Code Playgroud)
和ServiceInterface.getEndpoint("firstQueryParam", null);来电:
http://targethost.com/service/endpoint?queryA=firstQueryParam
Run Code Online (Sandbox Code Playgroud)
还有沃拉!没有第二个查询参数!:)
如果您的API需要的原始值(如int,float,boolean,等),然后使用该对象包装类(自动装箱为原始的(像)Integer,Float,Boolean,等).然后,您可以传递null给方法:
public Response getEndpoint(@QueryParam("queryA") Boolean first);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29159 次 |
| 最近记录: |