Reputation: 2279
I have a service which supports multiple operations A,B and C.
Each operation has different parameters. My architect wants me to implement these operations with following URL, for each operation:
http://<server>:<port>/application/service?q=A&<query_paramers>
http://<server>:<port>/application/service?q=B&<query_paramers>
http://<server>:<port>/application/service?q=C&<query_paramers>
i.e. he wants me to accept operation name as a query parameter and strictly not as path parameter.
Now the problem is each operation has same base URL i.e
http://<server>:<port>/application/service
As per my understanding, if i need to support, this format then in my service it will a single method which will be exposed as resource and in that method based on value of "q", i will decide which operation to invoke.
Though it seems it will work, but I have few concerns with this approach
A single resource representing all three operations will be exposed, which does not look correct to me. I am not sure of the industry standards about this but I think there should be one to one mapping between resources and application operations/behaviors.
A single resource will take parameters for all the operations, which means grammar will contain all the parameters listed under a single resource and user will have no way to find out which parameter belongs to which operation.
One work around to accept all parameters for 3 operation in a single method would be, my method takes a single @Context UriContext parameter but in this case, my grammar will list no parameters, which again is wrong as user will get no infomation at all regarding the possible parameters for each operation.
Thanks in advance.
Upvotes: 0
Views: 665
Reputation: 19002
Your thoughts are correct: if the so called "architect" said to you to make that ONLY WITH GET
Requests (or a single HTTP method), then your only way is to have a single resource method, where based on the value of parameter q
you call some different methods.
Related to your concerns:
UriInfo
instance, if you get one injected using a parameter like @Context UriInfo uriInfo
(see the getQueryParameters()). The disadvantage is you will demarshall them manually and pass accordingly to some private methods with a clear signature.PS: Ask your architect about his reasons to do that. If you change data in your Database, it is probably a bad idea to make GET requests (imagine one simply copies the URL and pastes it in the browser). Besides this is bad code, where you will debug more (imagine you will get later other 10 cases additional to q=a, q=b, q=c
).
Upvotes: 1
Reputation: 6855
What are the three operations? CRUD operations ?
query parameter in terms of REST are treated as a GET resource with specifications (where clause to be specific)
Its not recommended to give all the type of operations to same method with query param.
Change your path using @Path. If you want all the services to be hosted under the same path, then use Http methods to differentiate the operation.
If the operation is CREATE a resource...
Then Use @POST with the resource information as a parameter.
If you want to
Create the resource - @POST
retrieve the data - @GET
update the data - @PUT
delete the data - @DELETE
resources :
Upvotes: 0