蛋疼这个错误搞了一上午,终于解决:
1、*-servlet.xml文件加:
<mvc:annotation-driven />
2、请求报文头加:
Content-Type: application/json
用火狐的restful插件调试成功,但是用mac自带的curl工具始终失败郁闷
curl -H "Content-Type: application/json" -X POST -data '{"usname":"pageqiu"}' http://127.0.0.1:8080/ffan_train/testJson.do
errinfo:curl: (6) Could not resolve host: "usname"
先把正确的贴上来:
1、
*-servlet.xml文件
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scanbase-package="com.wanda.ffan.travel.train"/>
<beanid="jacksonMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<propertyname="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<!-- <value>application/json;charset=UTF-8</value>-->
</list>
</property>
</bean>
<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<propertyname="messageConverters">
<list>
<refbean="jacksonMessageConverter"/>
</list>
</property>
</bean>
<beanid="defaultHandlerMapping"class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<propertyname="defaultHandler">
<beanclass="org.springframework.web.servlet.mvc.UrlFilenameViewController"></bean>
</property>
</bean>
<beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass"value="org.springframework.web.servlet.view.InternalResourceView"/>
<propertyname="prefix"value="/WEB-INF/jsp/"/>
<propertyname="suffix"value=".jsp"/>
</bean>
</beans>
2、java
@RequestMapping(method=RequestMethod.POST,value="/testJson.do",consumes ="application/json")
public @ResponseBody String testJSON(@RequestBody DemoVovo){
log.error("***testJson begin****");
//DemoVo vo = (DemoVo) JSON.parse(body);
log.error(vo.getUsname());
log.error("*****testJson end****");
return "success";
}
用curl请求报错的原因也找到了:
curl -H "Content-Type: application/json" -X POST --data '{"usname":"pageqiu"}' http://127.0.0.1:8080/ffan_train/testJson.do
data前是两个-,开发容易吗?