2013-03-15 52 views
0


有没有更好的方法来传递多个JSON对象并在Spring 3中的@RequestBody中捕获它?我已经提到了this,但不希望为该目的定义新的包装类,就像该问题中所解释的那样?是Spring的限制还是REST限制(根据我的理解,这不应该是这种情况)?拼命地需要答案,并且因为我不能在同一个问题中发布额外的评论(已被删除),因此在此张贴。我们可以在REST URL中传递多个JSON对象并使用Spring @RequestBody捕获它?

谢谢,
水稻

回答

0

每个模型使用@JsonIgnoreProperties(ignoreUnknown =真)

@RequestMapping(value = "https://stackoverflow.com/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(); 
} 
相关问题