Send and parse JSON to spring controller?

Suppose if I have data JSONlike,

var json = {"name":"kite Player","age":"25","hobby":"footbal"}

I can send data JSONto

var jsonData = JSON.Stringfy(json);

In JQueryAjax,

data = jsonData ,

I can parse JSON data in spring controller,

public class TestController {
    @RequestMapping(method = RequestMethod.POST, value = "personDetails.html")
    public @ResponseBody Result math(@RequestBody final Persons persons) {

         String name = person.getName();
         String age = persons.getAge();
         String hobby = persons.getHobby();
         // Other process
    }
}

How can I make out JSONin Spring controller, if I need to send some information about a person in JSON, for example,

var json = [ {"name":"kite Player","age":"25","hobby":"footbal"},  
             {"name":"Steve","age":"40","hobby":"fishing"},
             {"name":"Marker","age":"28","hobby":"cricket"}
           ]

We hope that our members of the stack will give a good solution.

+1
source share
3 answers

This should work:

@RequestMapping(method = RequestMethod.POST, value = "personDetails.html")
public @ResponseBody Result math(@RequestBody List<Persons> personList) { ... }

- UNITED AND ADDED EXAMPLE -

I tested this locally and it works for me. Here's the code snippet:

public class TestController {
    public static class Test {                                                    
        String name;                                                                 

        public String getName() {                                                    
            return name;                                                                
        }                                                                            

        public void setName(String name) {                                           
            this.name = name;                                                           
        }                                                                            
    }                                                                             


    @RequestMapping(value = "/debug/test1.json", method = RequestMethod.POST)     
    @ResponseBody                                                                 
    public Test[] testList1(@RequestBody Test[] test) {                           
        return test;                                                                 
    }                                                                             

    @RequestMapping(value = "/debug/test2.json", method = RequestMethod.POST)     
    @ResponseBody                                                                 
    public List<Test> testList2(@RequestBody List<Test> test) {                   
        return test;                                                                 
    }                                                                     
}        

Here are the test results (I tested it with curl):

    Request:
    curl --header "Content-type: application/json" --header "Accept: application/json"  --data '[{"name": "John"}, {"name": "Jack"}]' http://localhost:8080/app/debug/test1.json
    Response:
    [{"name":"John"},{"name":"Jack"}]

    Request:
    curl --header "Content-type: application/json" --header "Accept: application/json"  --data '[{"name": "John"}, {"name": "Jack"}]' http://localhost:8080/app/debug/test2.json
    Response:
    [{"name":"John"},{"name":"Jack"}]

PS. - spring MVC, JSON , . , spring MVC . log4j.properties, , JSON :

log4j.logger.org.springframework.web.servlet.mvc.method.annotation=TRACE
+3

JsonObject Json Array, JSON. JSON .

GSON (google -json), .:)

0

@RequestMapping(method = RequestMethod.POST, value = "personDetails.html")
public @ResponseBody Result math(@RequestBody List< Persons > persons) {
    for (Persons person : persons) {
        String name = person.getName();
        String age = person.getAge();
        String hobby = person.getHobby();
        // Process the data
    }
}
0

All Articles