那些指向这些方法的uri会是什么样子

时间:2012-04-11 23:00:15

标签: java rest jersey

我在

看到了以下帖子

Handling multiple parameters in a URI (RESTfully) in Java

我只是好奇以下是一个有效的资源?

@Path("Client/{client}/users")
public class UserPage 
{
@GET
@Produces(MediaType.TEXT_HTML)
public String userChoice(@PathParam(value = "client") final String client) 
{****Method here which handles a list of 'users'****}

@GET
@Path("{name}")
@Produces(MediaType.TEXT_HTML)
public String userPage(@PathParam(value = "client") final String client,  @PathParam(value = "name") final String name)
{****Method here which handles 'user' information****}}

我特别好奇uri会称之为用户页面方法的{name}方法? {}如何在这里工作?我认为{}是用来包含路径类的名称,例如,如果类的路径是“/ Client”,那么它应该是{client}。 有什么建议 ? IDeas ??

1 个答案:

答案 0 :(得分:1)

您定义的资源足够有效,但根据您尝试执行的操作说明,您需要更改源代码,使其更像下图所示:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/Client/{client}/users")
public interface UserPage {
   @GET
   @Produces(MediaType.TEXT_HTML)
   public String userChoice(@PathParam(value = "client") final String client);

   @GET
   @Path("/{name}")
   @Produces(MediaType.TEXT_HTML)
   public String userPage(@PathParam(value = "client") final String client,
         @PathParam(value = "name") final String name);
}

假设根URI http://www.example.com:8080 ,则以下内容为true:

  • http://www.example.com:8080/Clients/10/users 上调用GET将 在资源实现类上调用userChoice方法。 client的值将 10
  • http://www.example.com:8080/Clients/10/users/Bob 上调用GET 将在资源实现类上调用userPage方法。 client的值将 10 name的值将为 Bob

花括号语法用于包含定义路径参数值的正则表达式。另外,请注意我在类级别路径中包含一个前导斜杠('/') - 省略它会导致路径不匹配(在大多数情况下)。