Resteasy Путь с выражениями, включая пробелы - PullRequest
1 голос
/ 19 декабря 2010

Я читаю книгу "ОТДЫХ Ява"; Сейчас я работаю с примерами из главы 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

Проблема с доступом / Клиенты / Sylvie-Van% 20der% 20Vaart. Причина:

Could not find resource for relative :

/ клиенты / Сильви-Ван% 20der% 20Vaart of полный путь: http://localhost:9095/customers/Sylvie-Van%20der%20Vaart

Я попытался просто добавить к регулярному выражению один пробел: @Path ("{first: [a-zA-Z] +} - {last: [a-zA-Z] +}").

1 Ответ

1 голос
/ 06 января 2011
[a-zA-Z]+([\\s]?[a-zA-Z]+)*

[a-zA-Z] + для имени

([\ s]? [A-zA-Z] +) * для дополнительных имен

Я думаю, что вышеупомянутое регулярное выражение будет обрабатывать случаи, которые вы спрашиваете.

...