30476

How to pass list parameters for each object using Spring MVC?

I am using Spring MVC RequestMapping here for GET parameters. Below is my code -

@RequestMapping(value = "index", method = RequestMethod.GET) public HashMap<String, String> handleRequest(@RequestParam("dc1Servers") String dc1Servers, @RequestParam("dc2Servers") String dc2Servers, @RequestParam("dc3Servers") String dc3Servers) { HashMap<String, String> model = new HashMap<String, String>(); String helloWorld = "Hello World!"; model.put("greeting", helloWorld); System.out.println(dc1Servers); System.out.println(dc2Servers); System.out.println(dc3Servers); return model; }

I am hitting this URL - http://127.0.0.1:8080/dataweb/index?dc1Servers=3&dc2Servers=3&dc3Servers=3 then it goes into the above code and prints out 3 on the console for all the print and works fine.

Now if you see, I have dc1, dc2 and dc3.

So for dc1, I would like to send these in the URL as the parameters-

dc1Servers=3 dc1HostA dc1HostB dc1HostC dc1IPAddresssA dc1IPAddresssB dc1IPAddresssC

Similarly for dc2, I would like to send these in the URL as the parameters-

dc2Servers=3 dc2HostA dc2HostB dc2HostC dc2IPAddresssA dc2IPAddresssB dc2IPAddresssC

Similarly for dc3, I would like to send these in the URL as the parameters-

dc3Servers=3 dc3HostA dc3HostB dc3HostC dc3IPAddresssA dc3IPAddresssB dc3IPAddresssC

Now I am not sure how would I construct an URL for this use case and how would my method will look like? I would like to send them in one single URL call.

Is this use case possible to do using Spring MVC?

Answer1:

You could pass the parameters any way you see fit, one way is to pass them by their natural grouping like this: http://127.0.0.1:8080/dataweb/index?dc1Server=dc1HostA,dc1IPAddressA&dc1Server=dcHostB,dc1IPAddressB....

By using the same parameter name for each group you'd need to use a MultiValueMap. A MultiValueMap is more or less a Map<String, List<String>>, so parameters with the same name(dc1Server, dc2Server, etc) would be in the same list. You would use this to get a full list of the params you passed in like this:

public HashMap<String, String> handleRequest(@RequestParam MultiValueMap<String,String> allRequestParams)

Then you'd have 3 keys, dc1Server would contain all the dc1Server data, dc2Server all the dc2Server etc

Could look into using @ModelAttribute if you want to do direct mapping to domain objects: Link

<strong>EDIT</strong>

public void testSpringMultiValueMap() { MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add("dc1Server", "dc1HostA,dc1IPAddressA"); map.add("dc1Server", "dc1HostB,dc1IPAddressB"); map.add("dc1Server", "dc1HostC,dc1IPAddressC"); map.add("dc2Server", "dc2HostA,dc2IPAddressA"); map.add("dc2Server", "dc2HostB,dc2IPAddressB"); map.add("dc2Server", "dc2HostC,dc2IPAddressC"); List<String> dc1List = map.get("dc1Server"); for(String pair: dc1List) { String[] tokens = pair.split(","); System.out.println("host: " + tokens[0] + " ip: " + tokens[1]); } }

Answer2:

Url parameters are strings, so your options for passing a variable number of strings are:

Encode your values in a single url parameter

The simplest way is to use delimiter characters if you know your values will never contain them. Given you're trying to pass ip addresses and host names, this should work for you: &dc1Servers=dc1HostA,dc1IPAddressA,dc1HostB,dc1IPAddressB, etc.

Pass the same url parameter multiple times

Put the @RequestParam annotation on a method arg of type String[], and pass that url parameter multiple times: &dc1Host=dc1HostA&dc1IPAddress=dc1IPAddressA&dc1Host=dc1HostB&dc1IPAddress=dc1IPAddressB, etc.

Here are some more details on this approach: How to pass post array parameter in Spring MVC

Recommend

  • Facebook Like Button doesn't increment
  • @Router in Spring Integration with annotations (request/reply)
  • why lock_guard can get an already locked mutex by unique_lock?
  • Control a pager (like the less program) from an extenal (parent) script
  • Sending dynamic email reminders in Ruby on Rails?
  • Facebook Kiosk Logout
  • Is it ok to submit a build to the app store for testing when a previous build is waiting for review?
  • How to pass a custom object in REST webservice
  • Can't send object to SOAP Web Service
  • Working example of paypal Website Payments Pro Hosted Solution Iframe?
  • iphone - Hide the annotation view in map view
  • Android C2DM - What happens if registration ID gets refreshed
  • AutoHotKey - how to send control and same key multiple times
  • download all file formats using angular and express JS
  • How can I send mail from my app with underline or bold text?
  • What is the difference between Socket.Send and Stream.Write? (in relation to tcp ip connections)
  • APNs messages are delivered but not received on iOS device
  • Reload UITableView from another tab
  • How to get ID of changed file on Google Drive
  • Peer to peer/adaptive payments with paypal [closed]
  • UrlEncode non-string properties for HTTP Post through HttpClient
  • FTPWebRequest .NET 3.5 vs 4
  • Python Paramiko send CTRL+C to an ssh shell
  • Send the string to its hex equivalent
  • JSON data through JS/AJAX into PHP
  • How to best manage SMTP clients
  • PHP get selected value of select form
  • Powershell Hash Table to HTML
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Compress a file with GZipStream while maintaining its meta-data
  • Cannot get text from text area
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • Why cepheus don't send int without quotes to orion?
  • iOS: Detect app start via notification press
  • Email verification using google app script and google forms
  • Get data from AJAX - How to
  • Using variable in a value field in jMeter
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Updating server-side rendering client-side
  • sending mail using smtp is too slow