【问题标题】:Binding the nested json to @RequestBody object using Jackson converter使用 Jackson 转换器将嵌套的 json 绑定到 @RequestBody 对象
【发布时间】:2011-07-15 03:39:10
【问题描述】:

我有两节课

public class Parent {
    private String name;
    private int age;
    private ArrayList<Child> children = new ArrayList<Child>();
    //Setters and getter to follow..
}

public Class Child {
    private String name;
    private int age;
}

Spring 配置包括:

<bean id="jsonMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter" />
        </list>
    </property>
</bean>  

控制器如下所示:

@RequestMapping(value = "/parents", 
                method = RequestMethod.POST, 
                headers="Content-Type=application/json")
public @ResponseBody Parent add(@RequestBody Parent parent, Model model) {

    logger.debug("Received request to add a parent");
    Parent tempParent = parentService.add(parent); // This will persist the parent object to the DB.. (Helper class)
    return tempContract;
}

在正常情况下,它应该将传入的json绑定到Parent,并在响应中将Parent作为Json返回。它给了我一个例外:“客户端发送的请求在语法上不正确。”使用以下输入 Json:

{
    "name" : "foo",
    "age" : "45",
    "children" : [ 
      {
         "name" : "bar",
         "age" : "15""
      },
      {
         "name" : "baz",
         "age" : "10""        
      }
    ]
} 

因此尝试更改 json,它可以很好地使用以下格式绑定 @RequestBody 和 @ResponseBody:

{
    "name" : "foo",
    "age" : "45",
    "children" : []
}

{
    "name" : "foo",
    "age" : "45",
    "children" : [ 
      {}
    ]
} 

所以我假设实际的子类或我传递 Json 对象 wrt Child 对象的方式有问题。有人可以帮我吗。另外,是否建议使用

 private ArrayList<HashMap<String, Child>> children = new ArrayList<HashMap<String, Child>>();

而不是

private ArrayList<Child> children = new ArrayList<Child>();

谢谢。

【问题讨论】:

    标签: java json spring binding jackson


    【解决方案1】:

    问题中更新的 JSON 示例仍然无效。我建议使用 JSONLint.com 检查 JSON 示例。

    以下是使用 Jackson 将 JSON 的更正版本反序列化为当前版本问题中相同的父/子类结构的示例。

    import java.util.ArrayList;
    
    import org.codehaus.jackson.map.ObjectMapper;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
    /*
    {
        "name": "foo",
        "age": "45",
        "children": [
            {
                "name": "bar",
                "age": "15"
            },
            {
                "name": "baz",
                "age": "10"
            }
        ]
    }
     */
        String jsonInput = "{\"name\":\"foo\",\"age\":\"45\",\"children\":[{\"name\":\"bar\",\"age\":\"15\"},{\"name\":\"baz\",\"age\":\"10\"}]}";
    
        ObjectMapper mapper = new ObjectMapper();
        Parent parent = mapper.readValue(jsonInput, Parent.class);
        System.out.println(mapper.writeValueAsString(parent));
        // output:
        // {"name":"foo","age":45,"children":[{"name":"bar","age":15},{"name":"baz","age":10}]}
      }
    }
    
    class Parent
    {
      public String name;
      public int age;
      public ArrayList<Child> children = new ArrayList<Child>();
    }
    
    class Child
    {
      public String name;
      public int age;
    }
    

    关于语法错误的消息是否可能只是:关于语法错误的消息?具体来说,关于无效的 JSON 输入?还是真正的 JSON 真的有效,只是问题中的例子无效?

    "是否建议使用ArrayList&lt;HashMap&lt;String, Child&gt;&gt;"

    没有。 JSON 结构与 ArrayList&lt;HashMap&lt;String, Child&gt;&gt; 不匹配。所以,我不会尝试使用它。

    【讨论】:

    • 由于某种原因,我发现了一个问题,我使用的 rest-client 使事情变得更糟。它现在正在工作。这是输入json的问题。不是应用程序本身。谢谢你们的帮助...
    【解决方案2】:

    人们会首先猜出孩子们 15 岁和 10 岁之后的流浪引号。是什么产生了您的意见?

    【讨论】:

    • 我更新了帖子。我带着引号过去了。而Json的后两种格式都成功了。
    【解决方案3】:

    通过添加以下逻辑解决了该问题。

    public @ResponseBody String  saveDrug(@RequestBody DrugListJsonRequest drugListCriterion) {}
    

    这里你的 Java 类 DrugListJsonRequest 应该扩展

    HashMap<String,ArrayList<DrugListCriterion>>
    

    它不需要任何方法和构造函数。

    public class DrugListJsonRequest extends HashMap<String,ArrayList<DrugListCriterion>>{}
    

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 1970-01-01
      • 2012-02-13
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2013-11-19
      • 1970-01-01
      相关资源
      最近更新 更多