Java - How to pass list in postman in GET request and get in GetMapping

Java - How to pass list in postman in GET request and get in GetMapping

Passing a list of values in a GET request using Postman and then retrieving it in a Spring Boot @GetMapping can be achieved in a couple of ways. The method you choose depends on how you want to format the request. Typically, lists in GET requests are passed as query parameters.

Method 1: Repeat Query Parameters

You can repeat the same query parameter for each value in the list. This is a common approach for passing arrays/lists in GET requests.

For example, if you want to pass a list of IDs, your URL might look like this:

http://localhost:8080/yourEndpoint?ids=1&ids=2&ids=3 

In your Spring Boot controller, you can retrieve this list as follows:

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class YourController { @GetMapping("/yourEndpoint") public String getMethod(@RequestParam List<String> ids) { // Your logic here return "Received IDs: " + ids; } } 

Method 2: Comma-Separated Values

Another approach is to pass the list as a single, comma-separated value.

For example, your URL might look like this:

http://localhost:8080/yourEndpoint?ids=1,2,3 

And in your Spring Boot controller:

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; import java.util.List; @RestController public class YourController { @GetMapping("/yourEndpoint") public String getMethod(@RequestParam String ids) { List<String> idList = Arrays.asList(ids.split(",")); // Your logic here return "Received IDs: " + idList; } } 

Using Postman

To test these endpoints with Postman:

  1. Open Postman and create a new GET request.
  2. Enter the URL of your endpoint.
  3. For Method 1: Add multiple query parameters with the same name (ids) and different values.
  4. For Method 2: Add a single query parameter (ids) with a comma-separated list of values.

Always remember to validate and sanitize your inputs in real applications to prevent issues like SQL injection or other types of attacks.

  1. Spring @GetMapping with list parameter:

    • Use @GetMapping with @RequestParam to handle a GET request with a list parameter.
    import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MyController { @GetMapping("/example") public String handleGetRequestWithList(@RequestParam List<String> values) { // Process the list of values return "Received values: " + values; } } 
  2. How to send a list in Postman to Spring Boot:

    • Java Spring MVC GET request with a list:

      • Use @GetMapping with @RequestParam to handle a GET request with a list parameter.
      import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MyController { @GetMapping("/example") public String handleGetRequestWithList(@RequestParam List<String> values) { // Process the list of values return "Received values: " + values; } } 
    • Postman GET request with array parameter:

      • Spring Boot @RequestParam list example:

        • Use @RequestParam with a list parameter to handle multiple values in a Spring Boot GET request.
        import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MyController { @GetMapping("/example") public String handleGetRequestWithList(@RequestParam List<String> values) { // Process the list of values return "Received values: " + values; } } 
      • Spring MVC GET mapping with array parameter:

        • Use @GetMapping with @RequestParam to handle a GET request with an array parameter.
        import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MyController { @GetMapping("/example") public String handleGetRequestWithList(@RequestParam List<String> values) { // Process the list of values return "Received values: " + values; } } 
      • Spring Boot GET request with a list of values:

        • Use @GetMapping with @RequestParam to handle a GET request with a list parameter.
        import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MyController { @GetMapping("/example") public String handleGetRequestWithList(@RequestParam List<String> values) { // Process the list of values return "Received values: " + values; } } 

More Tags

uikit keylistener pdf-form profiling comparison-operators datepicker tree ascii-art axis-labels elm

More Programming Questions

More Financial Calculators

More Auto Calculators

More Chemical reactions Calculators

More Gardening and crops Calculators