两个 rest 资源是否可以具有相同的方法和请求映射,但在 Restful 服务中具有不同的@path(url)


@Path("/users/A")
public class UserResource1 {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
...
}
}
@Path("/users/B")
public class UserResource2 {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
...
}
}

您可以使用相同的方法名称并使用不同的路径URL进行请求映射

/*
To handle A type users logic
http://localhost:8080/users/A
*/
@Path("/users/A") 
public class UserResource1 {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
}
}
/*
To handle B type users logic 
http://localhost:8080/users/B
*/
@Path("/users/B")
public class UserResource2 {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
}
}

最后你有两个终点

http://localhost:8080/users/A?username=bob

http://localhost:8080/users/B?username=testUser

最新更新