是否可以使用@RequestParam从JSON转换为域对象 [英] Is it possible to convert from JSON to domain object with @RequestParam

查看:1245
本文介绍了是否可以使用@RequestParam从JSON转换为域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从JavaScript脚本(AngularJS)执行多部分请求,我将JSON数据作为第一部分,将可选文件作为第二部分。是否可以将@RequestParam(data)自动从JSON转换为我的应用程序中的类?喜欢 @RequestParam(data)狗狗

I am doing a multipart request from a JavaScript script (AngularJS) and I get the JSON data as the first part, and an optional file as the second. Is it possible to have the @RequestParam("data") automatically converted from JSON to a class in my application? Like @RequestParam("data") Dog dog

推荐答案

是的。在对象之前使用@RequestBody注释( http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestbody ):

Yes. Use @RequestBody annotation before your object (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestbody):

public void jsonMethod(@RequestBody Dog dog)

注意:你必须让jackson将json转换为你的自定义对象。杰克逊maven依赖:

Note: you must have jackson to convert json to your custom object. Jackson maven dependency:

<dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-asl</artifactId>
   <version>1.7.1</version>
</dependency>

这篇关于是否可以使用@RequestParam从JSON转换为域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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