是否可以使用@RequestParam从JSON转换为域对象 [英] Is it possible to convert from JSON to domain object with @RequestParam
问题描述
我正在从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屋!