将 JSON Map 传递到 Spring MVC 控制器
2022-09-02 10:53:40
我正在尝试将 Map 的 JSON 表示形式作为 POST 参数发送到我的控制器中。
@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代替。
@RequestMapping(value = "/search.do", method = RequestMethod.GET, consumes = { "application/json" })
public @ResponseBody Results search(@ModelAttribute("filters") HashMap<String,String> filters, HttpServletRequest request) {
//do stuff
}
这将正确响应请求,但我意识到地图是空的。在后来的实验中,我发现任何对象(不仅仅是HashMap)都会被实例化,但不会填写任何字段。我的类路径上确实有 Jackson,我的控制器将使用 JSON 进行响应。但是,似乎我当前的配置不允许Spring通过GET / POST参数读取JSON。
如何将对象的JSON表示形式从客户端AJAX请求作为请求参数传递到Spring控制器,并取出Java对象?
编辑添加我的相关弹簧配置
<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 字符串用双引号括起来,这就可以了。
@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对象。