Pass array as parameter to JAX-RS resource

前端 未结 2 1910
刺人心
刺人心 2021-01-04 11:15

I have many parameters to pass to the server using JAX-RS.

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

相关标签:
2条回答
  • 2021-01-04 11:36

    We can get the Query parameters and corresponding values as 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);
    }
    
    0 讨论(0)
  • 2021-01-04 11:52

    You have a few options here.

    Option 1: A query parameter with multiple values

    You can supply multiple simple values for a single query parameter. For example, your query string might look like:

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

    Here the request parameter param1 has three values, and the container will give you access to all three values as an array (See Query string structure).

    Option 2: Supply complex data in the PUT body

    If you need to submit complex data in a PUT request, this is typically done by supplying that content in the request body. Of course, this payload can be xml (and bound via JAXB).


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

    Having said that, unless you really do just need an array of simple values, I'd recommend choosing the Option 2. I can't think of a good reason to use URL-encoded XML in the URL, but I'd be interested to hear more about exactly what this data is.

    0 讨论(0)
提交回复
热议问题