Using the same url of the resource, is it possible to call different methods underneath depending the arguments?
@POST
@Path("/resource/add")
@Consumes(MediaType.APPLICATION_JSON)
public Response add(Class1 arg1);
@POST
@Path("/resource/add")
@Consumes(MediaType.APPLICATION_JSON)
public Response add(Class2 arg2);
No, it is not possible because the container has no information on how to route the request.
As pointed out by @Jim Garrison in the comments, you could work around this by differentiating the path. However, I find this somewhat counter-intuitive. As far as I understand, it's not a different resource at all. You just want to use a different representation.
If you really want to introduce such logic, maybe you should introduce your own, custom media types for specific formats and use them instead of the generic application/json
The clients of your API would have to be conscious of this design decision, though. If you're unsure whether the introduction of custom media types is a good idea, take a look at the answers to these questions, which may make it a bit clearer:
If you don't want to have the representations different, then I don't really understand the point of having those two classes. Perhaps this division should not reach your RESTful API and you should be using these representations internally. In this case, you might want to implement an adapter, a decorator or maybe a factory to be able to switch between the two implementations. It's difficult to recommend a specific pattern without knowing how these classes are supposed to be used and what they represent.
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