Search code examples
javarestful-urlspring-restcontrollerspring-annotations

Can two rest resources have same methods and request mapping but different @path(url) in Restful service


@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) {
        ...
    }
}

Solution

  • you can have same method name and request mapping with different path 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) {
    
        }
    }
    

    finally you have two end points

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

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