Is it possible to set multiple @Path annotation on the same REST method in Java?
Obviously I have tried this however it did not work it failed to compile, but is there some way to do this? Perhaps vie the regular expression?
I am using resteasy if that helps.
My methods look like this (pseudo code below):
@Path("/project/{projecID}/car/{carID}/carService/{carserviceID}/engine/{engineID}")
public Engine getCarEngin(@PathParam("projecID") projectID, @PathParam("carID") carID, @PathParam("carserviceID") carserviceID, @PathParam{engineID}){
// return engine based on the id
}
@Path("/project/{projecID}/bus/{busID}/busService/{busserviceID}/engine/{engineID}")
public Engine getBusEngin(@PathParam("projecID") projectID, @PathParam("busID") busID, @PathParam("carserviceID") carserviceID, @PathParam{engineID}){
// return engine based on the id
}
So the method is the same, the logic is the same but so I would like to unite this under one method but keep the two paths to it.
As you have discovered That’s not possible.
You could do it with regular expressions, but that would just introduce a lot of unnecessary complexity. It would also reduce the readability of your
@Path-annotations. IMHO, don’t use regular expressions for this.Perhaps you can do something like this: