我有一个休息的服务,看起来像:
@GET
@Path("get-policy/{policyNumber}/{endorsement}/{type}")
@Produces(MediaType.APPLICATION_XML)
public String getPolicyIndividual(
@PathParam("policyNumber")String policyNumber,
@PathParam("endorsement")String endorsement,
@PathParam("type")String type){
...
}
我想知道是否有一种方法可以在不发送参数的情况下接受每个参数为空值,因此,如果某人在没有参数的情况下调用我的服务,或者不是所有参数仍然可以匹配我的定义服务。
例子:
http://localhost:8080/service/policy/get-policy/
或这个:
http://localhost:8080/service/policy/get-policy/5568
或这个:
http://localhost:8080/service/policy/get-policy/5568/4
我很清楚我可以像这样在answer中定义一个正则表达式,但是在那种情况下,只定义了1个路径参数,如果我有多个路径参数该怎么办?
那对我没有用,但也许我做错了事,但我没有成功尝试:
@GET
@Path("get-policy/{policyNumber: .*}/{endorsement: .*}/{type: .*}")
@Produces(MediaType.APPLICATION_XML)
public String getPolicyIndividual(
@PathParam("policyNumber")String policyNumber,
@PathParam("endorsement")String endorsement,
@PathParam("type")String type){
...
}
实现低谷的唯一方法是POST?我正在使用泽西岛!
最佳答案
如果您不想多次编写代码,则必须为此创建一个完整的用例场景,并每次都调用一个通用方法。
说:对于一个实例,仅使用传递的一个参数,然后是2,然后是全部,而没有一个
@GET
@Path("get-policy/{policyNumber: .*}")
@Produces(MediaType.APPLICATION_XML)
public String getPolicyIndividual(
@PathParam("policyNumber")String policyNumber)
{
doSomething(policyNumber, "", "");
}
@GET
@Path("get-policy/{policyNumber: .*}/{endorsement: .*}")
@Produces(MediaType.APPLICATION_XML)
public String getPolicyIndividual(
@PathParam("policyNumber")String policyNumber,
@PathParam("endorsement")String endorsement)
{
doSomething(policyNumber,endorsement, "");
}