1

我正在阅读《RESTful Java》一书;现在我正在处理第 4 章中的示例。我想更改表达式以接受带有空格的名称。我已将表达式更改为“[a-zA-Z]+”,但它不起作用。有没有办法使这项工作?

非常感谢

   @GET
   @Path("{first : [a-zA-Z]+}-{last:[a-zA-Z]+}")
   @Produces("application/xml")
   public StreamingOutput getCustomerFirstLast(@PathParam("first") String first,
                                               @PathParam("last") String last)
   {
    System.out.println(String.format("Parameters. First=%s; Last=%s", first, last));
      Customer found = null;
      for (Customer cust : customerDB.values())
      {
         if (cust.getFirstName().equals(first) && cust.getLastName().equals(last))
         {
            found = cust;
            break;
         }
      }
      if (found == null)
      {
         throw new WebApplicationException(Response.Status.NOT_FOUND);
      }
      final Customer customer = found;
      return new StreamingOutput()
      {
         public void write(OutputStream outputStream) throws IOException, WebApplicationException
         {
            outputCustomer(outputStream, customer);
         }
      };
   }

编辑:我不清楚。

当我尝试 URL:/customers/Sylvie-Van%20der%20Vaart 时,我收到以下错误:

HTTP 错误 404

访问 /customers/Sylvie-Van%20der%20Vaart 时出现问题。原因:

Could not find resource for relative :

/customers/Sylvie-Van%20der%20Vaart 的全路径: http://localhost:9095/customers/Sylvie-Van%20der%20Vaart

我试图简单地在正则表达式中添加一个空格:@Path("{first : [a-zA-Z ]+}-{last:[a-zA-Z ]+}")。

4

1 回答 1

1
[a-zA-Z]+([\\s]?[a-zA-Z]+)*

[a-zA-Z]+ 代表名字

([\s]?[a-zA-Z]+)* 用于可选的更多名称

我认为上面的正则表达式将处理您所询问的情况。

于 2011-01-06T17:33:49.247 回答