Bagira
Bagira

Reputation: 2279

JAX-RS: Implement multiple operations through query parameters

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

  1. 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.

  2. 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.

  3. 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

Answers (2)

V G
V G

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:

  1. You will have a single Resource METHOD that handles those three requests, which is bad. But it would not be bad if you had a single RESOURCE (i.e class) that handles all those requests with three resource methods.
  2. That is correct. The advantage of this soltion is that the demarshalling of parameters is automatically made for you (e.g you will get directly a Long parameter or an Enum, without decoding them manually).
  3. You can get all GET parameters from a 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

Vinay Veluri
Vinay Veluri

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 :

  1. link gives you detailed information about the HTTP methods.
  2. link gives the example with how to use the @Path and http methods

Upvotes: 0

Related Questions