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){
          ...
        }

而且我想知道是否有一种方法可以让我接受每个参数作为空值,如果它们没有被发送,所以如果有人在没有参数的情况下调用我的服务,或者不是所有的参数仍然可以匹配我的定义服务。例子:

http://localhost:8080/service/policy/get-policy/

或这个:

http://localhost:8080/service/policy/get-policy/5568

或这个:

http://localhost:8080/service/policy/get-policy/5568/4

我很清楚我可以在这个答案中定义一个正则表达式,但在那种情况下,只定义了 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 实现这一目标的唯一方法吗?顺便说一句,我正在使用泽西岛!

4

1 回答 1

1

如果您不想多次编写代码,则必须为此创建一个完整的用例场景并每次调用一个通用方法。说:对于一个实例,只使用一个传递的参数,然后是 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, "");
            }
于 2013-11-11T15:56:00.133 回答