Is it possible to define same path for two classes?
@Path("/resource")
public class ResourceA{
..
..
}
@Path("/resource")
public class ResourceB(){
..
..
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With