Search code examples
spring-bootpostmanhttp-request-parameters

How to pass String array in @RequestParam in REST GET API call through POSTMAN or DHC REST application?


I have following REST controller in Java Spring Application:

@RequestMapping(
     value = "/api/getApplication/{me_userId}", 
     method = RequestMethod.GET, 
     produces = MediaType.APPLICATION_JSON_VALUE)
public Object getApplication(
        @PathVariable String userId,
        @RequestParam(value="fieldNames[]", required = false) String[] fieldNames) {

            if (fieldNames != null) {
                for (String fieldName : fieldNames)
                    System.out.println(fieldName);
            }

            ...
            return null;
}

So I can not succeed in simulating API call from DHC REST of POSTMAN what will pass that fieldNames[].

Does anyone know how to do it?


Solution

  • First of all, your current method does not work because your @PathVariable is wrong. In your @RequestMapping you have the following placeholder in your path: {me_userId}, which means that it will be mapped to a path variable with that name.

    However, the only @PathVariable you have is nameless, which means it will use the name of the parameter (userId) in stead.

    So before you try to execute your request, you have to change your @RequestMapping into:

    @RequestMapping(
        value = "/api/getApplication/{userId}", // <-- it's now {userId}
        method = RequestMethod.GET, 
        produces = MediaType.APPLICATION_JSON_VALUE)
    

    Then, if you run the application, you can kinda choose how you want to pass your parameters. Both the following will work:

    ?fieldNames[]=test,test2
    

    Or:

    ?fieldNames[]=test&fieldNames[]=test2
    

    Both these results should print the desired results.