问题描述
我正在尝试将地图的JSON表示作为POST参数发送到我的控制器中.
I'm trying to send a JSON representation of a Map into my controller as a POST parameter.
@RequestMapping(value = "/search.do", method = RequestMethod.GET, consumes = { "application/json" })
public @ResponseBody Results search(@RequestParam("filters") HashMap<String,String> filters, HttpServletRequest request) {
//do stuff
}
我发现@RequestParam只会抛出500错误,因此我尝试使用@ModelAttribute.
I found that @RequestParam would just throw a 500 error, so I tried using @ModelAttribute instead.
@RequestMapping(value = "/search.do", method = RequestMethod.GET, consumes = { "application/json" })
public @ResponseBody Results search(@ModelAttribute("filters") HashMap<String,String> filters, HttpServletRequest request) {
//do stuff
}
这将正确响应请求,但是我意识到Map是空的.在以后的实验中,我发现将实例化任何对象(不仅是HashMap),而且不会填充任何字段.我的类路径中确实有Jackson,并且我的控制器将使用JSON进行响应.但是,似乎我的当前配置不允许Spring通过GET/POST参数读取JSON.
This would correctly respond to requests, but I realized that the Map was empty. With later experimentation, I found that any object (not just HashMap) would be instantiated, but no fields would be filled in. I do have Jackson on my classpath, and my controllers will respond with JSON. However, it would appear that my current configuration is not allowing Spring to read JSON in via a GET/POST parameter.
如何将对象的JSON表示形式从客户端AJAX请求传递给Spring控制器作为请求参数,并获取Java对象?
How does one pass JSON representations of objects from a client-side AJAX request to a Spring controller as a request parameter and get a Java object out?
编辑,添加我的相关Spring配置
EDIT Adding my relevant Spring configuration
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="true" />
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
在评论者的建议下,我尝试了@RequestBody.只要JSON字符串用双引号加引号,就可以使用.
On the suggestion of a commenter, I tried @RequestBody. This will work, so long as the JSON strings are quoted with double quotes.
@RequestMapping(value = "/search.do", method = RequestMethod.POST, consumes = { "application/json" })
public @ResponseBody Results<T> search(@RequestBody HashMap<String,String> filters, HttpServletRequest request) {
//do stuff
}
这确实解决了我眼前的问题,但我仍然对ou如何通过AJAX调用传递多个JSON对象感到好奇.
This does solve my immediate issue, but I'm still curious as to how ou might pass in multiple JSON objects via an AJAX call.
推荐答案
执行此操作的最佳方法是使包装器对象包含要传递的两个(或多个)对象.然后,您将JSON对象构造为两个对象的数组,即
The best way to do this is to have a wrapper object that contains the two (or multiple) objects you want to pass. You then construct your JSON object as an array of the two objects i.e.
[
{
"name" : "object1",
"prop1" : "foo",
"prop2" : "bar"
},
{
"name" : "object2",
"prop1" : "hello",
"prop2" : "world"
}
]
然后在控制器方法中将请求正文作为一个对象接收,并提取两个包含的对象.即:
Then in your controller method you recieve the request body as a single object and extract the two contained objects. i.e:
@RequestMapping(value="/handlePost", method = RequestMethod.POST,
consumes = { "application/json" })
public void doPost(@RequestBody WrapperObject wrapperObj) {
Object obj1 = wrapperObj.getObj1;
Object obj2 = wrapperObj.getObj2;
//Do what you want with the objects...
}
包装对象看起来像...
The wrapper object would look something like...
public class WrapperObject {
private Object obj1;
private Object obj2;
public Object getObj1() {
return obj1;
}
public void setObj1(Object obj1) {
this.obj1 = obj1;
}
public Object getObj2() {
return obj2;
}
public void setObj2(Object obj2) {
this.obj2 = obj2;
}
}
这篇关于将JSON映射传递到Spring MVC Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!