Reputation: 723
Is it possible to define same path for two classes?
@Path("/resource")
public class ResourceA{
..
..
}
@Path("/resource")
public class ResourceB(){
..
..
}
Upvotes: 4
Views: 6345
Reputation: 209112
It is possible. See the JAX-RS Spec 3.7.2 Request Matching. In layman's terms, the spec states that all matching root resource classes are put into a set, then all the matching methods from those classes are put into a set. Then sorted. So if the resource class level @Path
s are the same, they will both be put into the set for further processing
You can easily test this out, as I have done below (with Jersey Test Framework)
public class SamePathTest extends JerseyTest {
@Test
public void testSamePaths() {
String xml = target("resource").request()
.accept("application/xml").get(String.class);
assertEquals("XML", xml);
String json = target("resource").request()
.accept("application/json").get(String.class);
assertEquals("JSON", json);
}
@Path("resource")
public static class XmlResource {
@GET @Produces(MediaType.APPLICATION_XML)
public String getXml() { return "XML"; }
}
@Path("resource")
public static class JsonResource {
@GET @Produces(MediaType.APPLICATION_JSON)
public String getJson() { return "JSON"; }
}
@Override
public Application configure() {
return new ResourceConfig(XmlResource.class, JsonResource.class);
}
}
Upvotes: 10
Reputation: 46
Isn't possible, you have that to separate with the methods.
Example:
@Path("/resource")
public class Resource{
@Path("/A")
public void resourceA(){
...
}
@Path("/B")
public void resourceB(){
...
}
..
}
You can access resourceA with the url "/resource/A" and resourceB with "/resource/B".
I hope this help.
Upvotes: -1