java - 将 JSON 发送并解析到 spring Controller ?

标签 java json spring jquery spring-mvc

假设,如果我有 JSON 数据,

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

我可以通过

发送JSON数据
var jsonData = JSON.Stringfy(json);

JQueryAjax 中,

data = jsonData ,

我可以通过以下方式在 spring Controller 中解析 JSON 数据,

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
    }
}

我如何在 Spring Controller 中解析 JSON,如果我需要在 JSON 中发送多个人的详细信息,例如,

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

希望栈友们给出好的解决方案。

最佳答案

这应该有效:

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

--编辑和添加的示例--

我已经在本地对此进行了测试,它适用于我。这是代码片段:

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;                                                                 
    }                                                                     
}        

下面是测试结果(我是用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"}]

附言。有时,当 JSON 请求在到达 Controller 之前失败时,很难在 spring MVC 中获取任何调试信息。要获取调试信息,在某些情况下,您需要设置 spring MVC 的调试级别以进行跟踪。当我需要验证 JSON 请求失败的原因时,我通常将其添加到我的 log4j.properties 中:

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

关于java - 将 JSON 发送并解析到 spring Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17613927/

相关文章:

java - Spring 事务不回滚

java - Android 中的模拟位置数据

java - 为一项任务预初始化数据的设计模式

java - 当我使用应用程序 json 查看器打开 JSON 文件时,它为空

java - 如何让Android应用程序在安装时有一个包含图像的文件夹?

ios - Xcode 中来自 JSON 的嵌套 NSDictionary

spring - 使用 Spring Security 保护 REST 微服务

java - 动态填充 thymeleaf 中的列表

java - 从java中的json项目中获取值(value)

JavaScript/jQuery JSON 数组问题 - 值未定义