【发布时间】:2014-06-07 08:11:57
【问题描述】:
当我从 Chrome Rest 客户端传递应用程序/json 参数时,我收到 400 bad request 错误。 当我为 @RequestParam 添加 required=false 时,请求被 Controller 接受,但值为 Null。
@RequestMapping(value = "/add",
method = RequestMethod.POST,
consumes="application/json",
produces="application/json")
public @ResponseBody String add(
@RequestParam(value="surveyName") String surveyName,
@RequestParam(value="surveyDesc") String surveyDesc,
ModelMap model) throws Exception
{
System.out.println("request parameters in /add/syrvey surveyName= "+surveyName);
}
我的 JSON 请求如下,Content-Type 是“aplication/json”
{"surveyName"="sd", "surveyDesc":"sd"}
我尝试使用 headers="Accept=application/json",但没有太大帮助。
我的 dispatcher-servlet 是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=">
<context:annotation-config />
<context:component-scan base-package="com.survey.controller" />
<context:component-scan base-package="com.survey.service" />
<context:component-scan base-package="com.survey.dao" />
<context:component-scan base-package="com.survey.entity" />
<context:component-scan base-package="com.survey.constants" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
</beans>
我的 pom.xml 是
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
非常感谢任何帮助
【问题讨论】:
-
"surveyName"="sd" 不应该是 "surveyName":"sd" 吗?
-
感谢您的快速回复!抱歉,打错字了,我把它改成了 "surveyName":"sd" 问题依然存在
标签: spring spring-mvc http-status-code-404 rest-client