insomiac
insomiac

Reputation: 5664

Same Path for two resources in jersey

Is it possible to map same path for two resources ?

Ex: Resource1.java

@Path("/users")

Ex : Resource2.java

@Path("/users")

Is this possible ? Both the classes have different sub paths still it fails and give me 500 internal server error with servlet init error.

Upvotes: 5

Views: 6211

Answers (5)

DaShaun
DaShaun

Reputation: 3880

I think you actually can have the same path if you change something else about the request.

If I change the @produces and @consumes on each method I can, for instance, return XML for one of the methods and JSON for the other.

@produces(Application.XML)
@Path("/path")
public void methodA();

@produces(Application.jSON)
@Path("/path")
public void methodB();

Upvotes: 4

Tyler Zale
Tyler Zale

Reputation: 634

I'd recommend using one class mapped to jersey and have 2 helper classes that you delegate to to keep your code clean.

Upvotes: 2

user1596371
user1596371

Reputation:

If the paths have different subpaths then you shoud specify the paths more fully in the separate @Path attributes, for example:

@Path("/users/{id:[a-z0-9]+}/sub1/")

@Path("/users/{id:[a-z0-9]+}/sub2/")

If you cannot specify them to the extent that it is clear which resource to call given any specific path then Jersey won't be able to decide which resource to call.

Upvotes: 4

Perception
Perception

Reputation: 80593

The request matching rules specified in section 3.7.2 of the specification basically presume that each resource class will have an unambiguous, unique @Path expression associated with it.

To be honest, if you find yourself needing to specify the same path for two different resources then you should probably merge the resources.

Upvotes: 8

Oleksi
Oleksi

Reputation: 13097

Jersey will give you an error if you make a request where multiple resources might be able to respond. That is, the resources have ambiguous paths. However, I don't think that's the error you're getting here, if the overall path is not-ambiguous as you say.

Upvotes: 1

Related Questions