Reputation: 41
I'v been asked to implement a request for a REST Web Service, with dynamic parameters. I'll explain with an example: now our request object's fields are three strings, which are taken from the controller and used to realize the business logic. Tomorrow, we may have the need to introduce another parameter, so I'v been suggested to implement the request object with a Map, so that we can manage more than 3 properties in the request, without opening the code and having to re-deploy the service.
Now, my question is, is this possible? I think it is, but anyways the controller will not know what to do with the newly inserted properties, or maybe it will never use it! So, IMHO, this is a useless rework, because we will need to open the code and redeploy the .war anyhow.
Thanks in advance for your help.
P.S. The web service is a wrapper for GraphDB calls, if it helps
Upvotes: 1
Views: 1990
Reputation: 2404
If you are using GET method, you can get parameter names to values as shown in below snippet:
@GET
public String get(@Context UriInfo ui) {
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
MultivaluedMap<String, String> pathParams = ui.getPathParameters();
}
For Form parameters it is possible to do the following:
@POST
@Consumes("application/x-www-form-urlencoded")
public void post(MultivaluedMap<String, String> formParams) {
// Store the message
}
By using MultivaluedMap, you can pass any parameters to the Rest service and you can process your business logic.
Reference: Extracting request params
Upvotes: 1