當我從Chrome Rest Client傳遞應用程序/ json參數時,出現400錯誤請求錯誤。 當我爲@RequestParam添加required = false時,請求被Controller接受,但值爲Null。彈簧控制器不接受應用程序/ json
@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"}
我試圖用頭= 「接受=應用程序/ JSON」,但它並沒有太大的幫助。
我的調度員的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」 ? – Prasad
感謝您的快速回復!對不起,這是一個錯字,我將它改爲「surveyName」:「sd」,問題仍然存在 – madhuram2468
發佈日誌跟蹤。 – Prasad