将JSON发送并解析为弹簧控制器? [英] Send and parse the JSON to spring controller?

查看:163
本文介绍了将JSON发送并解析为弹簧控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,如果我有 JSON 等数据,

Suppose , If I have the JSON data like ,

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

我可以发送 JSON 数据

var jsonData = JSON.Stringfy(json);

JQueryAjax

data = jsonData ,

我可以在Spring控制器中解析JSON数据,

I can parse the JSON data in the spring controller by,

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控制器中解析 JSON ,如果我需要在 JSON中发送多个人的详细信息喜欢,

How can I parse the JSON in the Spring controller, If I need to send multiple person details in JSON like ,

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

希望我们的堆栈成员能给出一个很好的解决方案。

Hope our stack members will give a good solution.

推荐答案

这应该有效:

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

- 编辑和添加示例 -

--EDITED AND ADDED EXAMPLE--

我在本地进行了测试,它对我有用。这是代码片段:

I've tested this locally and it works for me. Here's 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;                                                                 
    }                                                                     
}        

以下是测试结果(我用curl测试过它) ):

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。在JSON请求到达控制器之前失败时,很难在spring MVC中获取任何调试信息。要获取调试信息,在某些情况下,您需要将spring MVC的调试级别设置为trace。当我需要验证JSON请求失败的原因时,我通常会将此添加到我的log4j.properties中:

PS. Somethime it's hard to get any debug information in spring MVC when JSON request fails before it reaches controller. To get the debug info, in some cases you need to set debug level of spring MVC to trace. I usually add this to my log4j.properties when I need to verify why a JSON request failed:

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

这篇关于将JSON发送并解析为弹簧控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆