Hello. How to accept and read an array of strings using Spring 3 ? The JSON request is an array of strings. It looks like this:

 ["cnh","dfd","asd"] 

Here is what it takes on the server:

 @RequestMapping(value = "/deleteelements.form") @ResponseBody public String deleteElements(/* ??? */) { } 

    2 answers 2

    Try

     @RequestMapping(value = "/deleteelements.form") @ResponseBody public String deleteElements(@RequestBody String elements[]) { } 

    Of course, for this Spring must see the Jackson library. In the case of maven, add dependencies:

      <!-- Jackson JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-lgpl</artifactId> <version>1.8.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-lgpl</artifactId> <version>1.8.4</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> 

    Or just throw the appropriate jar-nicknames in WEB-INF / lib.

    In addition, for an incoming HTTP request, the Content-type header must match application/json .

    • I will add more. You can also like this: @RequestBody ArrayList <String> str - Anton Mukhin
    • one
      you can even HttpEntity<String[]> . Spring MVC 3 in this regard generally gives a lot of tasty. This is what you can input to the controller methods. - Nofate

    UPD In fact, integration with Jackson is implemented in Spring ( MappingJacksonHttpMessageConverter ), you just need to connect its libraries, see the answer @Nofate.


    In order for the @Nofate option to work, you must first add a converter, since there is no converter for arrays of strings in Spring. SO shows how this can be done (configuration should seem to be simplified in Spring 3.1M1): in short, this is just an implementation of the interface / abstract class of the converter and its registration.

    In the converter itself, you can use, for example, Jackson , for conversion.

    If you only need it once, you can do it directly:

     @RequestMapping(value = "/deleteelements.form") @ResponseBody public String deleteElements(@RequestBody byte[] data) { // делает то же самое, что делали бы конвертере, // скармливаем Jackson'у байтовый массив } 
    • Isn't Jackson automatically used if JSON comes in? - Anton Mukhin
    • Used by You only need to throw it in the dependency / library. If Jackson is in the project, the spring itself will comprehend everything and parse JSON into an array. - Nofate
    • @Nofate, tell me, please, how to do it. - Anton Mukhin
    • If you are using maven: <! - Jackson JSON -> <dependency> <groupId> org.codehaus.jackson </ groupId> <artifactId> jackson-core-lgpl </ artifactId> <version> 1.8.4 </ version> </ dependency> <dependency> <groupId> org.codehaus.jackson </ groupId> <artifactId> jackson-mapper-lgpl </ artifactId> <version> 1.8.4 </ version> </ dependency> <dependency> <groupId> commons-collections </ groupId> <artifactId> commons-collections </ artifactId> <version> 3.2.1 </ version> </ dependency> - Nofate
    • one
      If you do not use, just throw jar-nicks in WEB-INF / lib to other libraries. - Nofate