title: Passing a String[] from react to springboot
body: <p>I want to pass a <code>String</code> array of usernames from react to Spring so that I can then get some user details for each of those usernames and finally pass this back to react as a <code>List&lt;String&gt;</code></p>
  <p>So far in react, I am making an array of usernames, and then passing them as the request body to spring</p>
  <pre><code>        const roundRobin = () =&gt; {
          const userList = []
          //Get list of entrants usernames to pass to backend
          for(let i =  0; i &lt; entrants.length; i++){
            userList.push(entrants[i].username);
            console.log(userList);
          }
          const List = JSON.stringify(userList) 
  
          //API call
          apiCalls
          .getRandomUserList(List)
          .then((response) =&gt; {
            console.log(response.data);
          })
          .catch((apiError) =&gt; {
            if (apiError.response.data &amp;&amp; apiError.response.data.validationErrors) {
              setEditErrors(apiError.response.data.validationErrors);
            }
            console.log(apiError.response.data)
            setPendingApiCall(false);
          });
          
        }
  </code></pre>
  <p>In spring my controller takes the request body as a <code>String[]</code></p>
  <pre><code>//create a random list of members who have entered an event
      @CrossOrigin
      @GetMapping(&quot;/users/createRandomList&quot;)
      List&lt;String&gt; randomList(@RequestBody String[] usernames) {
          return userService.createRandomUserList(usernames);
      }
  </code></pre>
  <p>The UserService then takes the <code>String[]</code> and changes it to a List and calls a method which randomly rearranges the order of the Strings, it then loops through the returned List (which are a username) and gets the <code>User</code> from the database and adds some details about that User to a new List This is then returned to react.</p>
  <pre><code>public List&lt;String&gt; createRandomUserList(String[] randomUsernames) {
          List&lt;String&gt; users = new ArrayList&lt;&gt;();
          List&lt;String&gt; randomUsersList = Arrays.asList(randomUsernames);
          List&lt;String&gt; randomUsers = getRandomUsers(randomUsersList);
          for (String randUsernames : randomUsers) {
              User u = userRepository.findByUsername(randUsernames);
              users.add(u.getFirstname() + &quot; &quot; + u.getSurname() + &quot; &quot; + u.getHandicap());
          }
          return users;
  
      }
  
      //Create list of entrants IDs in random order for tee times.
      public List&lt;String&gt; getRandomUsers(List&lt;String&gt; userIds) {
          int size = userIds.size();
          List&lt;String&gt; passedList = userIds;
          List&lt;String&gt; entrants = new ArrayList&lt;&gt;();
          Random rand = new Random();
          for(int i = 0; i &lt; size; i++) {
              int randomIndex = rand.nextInt(passedList.size());
              entrants.add(passedList.get(randomIndex));
              passedList.remove(randomIndex);
          }
          return entrants;
      }
  </code></pre>
  <p>When I try and run this in my web app though, I get an HTTP 400 error,</p>
  <pre><code>{timestamp: 1640902047907, status: 400, message: 'Required request body is missing: java.util.List&lt;j…ser.UserController.randomList(java.lang.String[])', url: '/api/1.0/users/createRandomList'}
  </code></pre>
  <p>I am not sure what I am doing wrong, as far as I can tell, I am passing an array to Spring, when I console.log(List), I get <code>[&quot;user1&quot;,&quot;user2&quot;]</code></p>