Pass the array as a parameter to the JAX-RS resource

I have many parameters that can be transferred to the server using JAX-RS.

Is there a way to pass or AarryList with a URL?

+10
arrays parameter-passing jax-rs
source share
2 answers

You have several options.

Option 1: query parameter with multiple values

You can provide several simple values ​​for one query parameter. For example, a query string might look like this:

PUT /path/to/my/resource?param1=value1¶m1=value2¶m1=value3

Here, the param1 query parameter has three values, and the container will give you access to all three values ​​as an array (see Query String Line ).

Option 2: provide complex data in a PUT package

If you need to send complex data to a PUT request, this is usually done by delivering this content to the request body. Of course, this payload can be xml (and linked via JAXB).


Remember that the point of the URI is the identification of the resource ( RFC 3986, 3.4 ), and if this array of values ​​is the data that is needed to identify the resource, then the URI is a good place to do this. If, on the other hand, this data array is part of a new view that is sent in this PUT request, then it belongs to the request body.

Having said that, if you really do not need an array of simple values, I would recommend choosing option 2. I can not come up with a good reason to use URL-encoded XML in the URL, d be interested to know more about what exactly this data is.

+7
source share

We can get request parameters and corresponding values ​​in the form of a Map,

 @GET @Produces(MediaType.APPLICATION_JSON) public void test(@Context UriInfo ui) { MultivaluedMap<String, String> map = ui.getQueryParameters(); String name = map.getFirst("name"); String age = map.getFirst("age"); System.out.println(name); System.out.println(age); } 
+1
source share

All Articles