@Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getfirstname() {
return "Hello Maclean";
}
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getlastname() {
return "Hello Pinto";
}
}
如上面的代码所示,有两种方法可以返回文本响应。如果我试试
localhost:8080/RestAPI/rest/hello
始终调用第一个方法。我阅读了一些文档,并了解到REST认为每个URL的资源是唯一的。这有效吗?我知道我可以通过向单个方法发送查询参数并根据查询参数发送不同响应的方法来实现此目的。所以任何人都可以提出一种方法,我可以通过网址完成此操作。没有添加查询参数和所有。
提前致谢。
答案 0 :(得分:2)
除了课程上的@Path
之外,您可能还需要在方法中添加@Path
,例如:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("firstname")
public String getfirstname() { ...
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("lastname")
public String getlastname() { ...
他们将可以访问:
localhost:8080/RestAPI/rest/hello/firstname
localhost:8080/RestAPI/rest/hello/lastname
答案 1 :(得分:1)
如果您不想为您的案例添加查询参数,可以在方法上使用@Path
。
@Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Path("/firstname")
@Produces(MediaType.TEXT_PLAIN)
public String getfirstname() {
return "Hello Maclean";
}
// This method is called if TEXT_PLAIN is request
@GET
@Path("/lastname")
@Produces(MediaType.TEXT_PLAIN)
public String getlastname() {
return "Hello Pinto";
}
}