title: Convert list of Objects to JSON Array in java - Spring Boot api testing
body: <p>when I try to make a mockmvc post request I have to pass a list of objects in the content tag, the problem is that everytime I try to pass it with <strong>this method</strong>:</p>
  <pre><code>public static String asJsonString(final Object obj) {
          try {
              final ObjectMapper mapper = new ObjectMapper();
              final String jsonContent = mapper.writeValueAsString(obj);
              return jsonContent;
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
  </code></pre>
  <p>I get this <strong>error</strong>:</p>
  <pre><code>{&quot;status&quot;:&quot;NOT_ACCEPTABLE&quot;,&quot;errors&quot;:{&quot;timestamp&quot;:&quot;2021-11-29T11:53:11.2020882Z&quot;,&quot;message&quot;:&quot;Wrong message format&quot;,&quot;details&quot;:&quot;JSON parse error: Cannot deserialize instance of `java.util.ArrayList&lt;Compania&gt;` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList&lt;Compania&gt;` out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 1]&quot;}}
  </code></pre>
  <p>The test I've made for saving only 1 object works well, but whenever I try to add a list it breaks, here you have my code for testing.</p>
  <pre><code>@Test
      void successSavePostCompaniaLista() throws Exception {
          Compania c1 = new Compania(&quot;Compania1 list&quot;,
              &quot;name1&quot;,
              &quot;---&quot;,
              &quot;---&quot;,
              null,
              null);
          Compania c2 = new Compania(&quot;Compania2 list&quot;,
              &quot;name2&quot;,
              &quot;---&quot;,
              &quot;---&quot;,
              null,
              null);
  
          List&lt;Compania&gt; companias = List.of(c1,c2);
          when(companiaRepository.save(any(Compania.class))).then(returnsFirstArg());
  
          this.mockMvc.perform(
                  post(&quot;/companias/lista&quot;)
                      .header(&quot;authorization&quot;, &quot;Bearer &quot; + token)
                      .content(asJsonString(companias)) //&lt;-- Here gives me errors
                      .contentType(MediaType.APPLICATION_JSON))
              .andDo(print())
              .andExpect(status().isCreated())
              .andExpect(jsonPath(&quot;$.result[0].success[0]&quot;).isNotEmpty())
              .andExpect(jsonPath(&quot;$.result[0].success[0].name&quot;).value(c1.getName()))
              .andExpect(jsonPath(&quot;$.result[0].success[1].name&quot;).value(c2.getName()));
  
      }
  </code></pre>
  <p>The one that works is similar to this one, but using only one object.</p>
  <p>The http direction and the rest is ok.</p>
  <p>Thanks!</p>
  <p>Edit. Here is the <strong>service</strong> I'm trying to test</p>
  <pre><code>public Map&lt;String, Object&gt; postListCompanias(List&lt;Compania&gt; companias) {
          for (int i = 0; i &lt; companias.size(); i++) {
              Compania companiaN = companias.get(i);
              companiaN.setId(null);
  
              companiaRepository.save(companias.get(i));
              System.out.println(companias.get(i));
          }
  
          Map&lt;String, Object&gt; mappedResult = Collections.singletonMap(
              &quot;result&quot;,
              List.of(
                  Collections.singletonMap(
                      &quot;success&quot;,
                      companias
                  )
              )
          );
          return mappedResult;
      }
  </code></pre>
  <p>And here the <strong>controller</strong>:</p>
  <pre><code>@ResponseStatus(HttpStatus.CREATED)
      @PostMapping(&quot;/lista&quot;)
      public Map&lt;String, Object&gt; createCompanias(@RequestBody List&lt;Compania&gt; companias) {
  
          return companiaService.postListCompanias(companias);
      }
  </code></pre>