我们可以在REST URL中传递多个JSON对象并使用Spring @RequestBody捕获它吗?

时间:2013-03-15 11:30:43

标签: spring


   有没有更好的方法来传递多个JSON对象并在Spring 3中的@RequestBody中捕获它?我曾提到this,但是不希望为此目的中解释的目的定义新的包装类?是Spring的限制还是REST限制(根据我的理解,情况应该不是这样)?迫切需要回答,因为我无法在同一个问题(已删除)中发布其他评论,因此将其发布在此处。

谢谢,
稻谷

1 个答案:

答案 0 :(得分:0)

对于每个模型使用@JsonIgnoreProperties(ignoreUnknown = true)

 @RequestMapping(value = "/users/testBean", method = RequestMethod.POST, consumes={"application/json","application/xml"}, produces={"application/json","application/xml"}) 
public @ResponseBody List<User> testBean(@RequestBody Object object) {
    System.out.println("testBean called");
    System.out.println(object.toString());

    ObjectMapper mapper=new ObjectMapper();

    User user =mapper.convertValue(object, User.class);
    Tree tree =mapper.convertValue(object, Tree.class);

    System.out.println("User:"+user.toString());
    System.out.println("Tree:"+tree.toString());
    return userService.findAll();
}