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

问题描述 投票:0回答:1
@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) {
        ...
    }
}
java restful-url spring-restcontroller spring-annotations
1个回答
0
投票

你可以有相同的方法名和请求映射不同的路径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:8080usersA?username=bob

http:/localhost:8080usersB?username=testUser。

© www.soinside.com 2019 - 2024. All rights reserved.