有两种方式,一种是以实体类方式接收
另一种方式是以Map方式接受,
发送的请求是
{"code":"aaa"}
Message里面包含code 字段,有get,set方法
第一种方式:
@RequestMapping(value = "/onLogin",method = RequestMethod.POST) //
public void postjson(HttpServletRequest req,HttpServletResponse rep ,@RequestBody Message map) throws Exception{
System.out.println("map:"+message.getCode());
第二种方式:map方式
@RequestMapping(value = "/onLogin",method = RequestMethod.POST) //
public void postjson(HttpServletRequest req,HttpServletResponse rep ,Map<String, String> map) throws Exception{
if(map.containsKey("code"){
String code = map.get("code").toString();
}
写完请求结果报错,
SpringMvc HTTP Status 415 ,The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
慢慢来,步骤一:
applicationContext.xml里面加入
<mvc:annotation-driven/>
另外
<beans>里面要加入
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation= "http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "
还得加入jaskson相关的jar包
版本大于2.6 不然会报错 ,jar包下载地址
http://down.51cto.com/data/2257291
参考资料:
http://www.jianshu.com/p/7e1432aad92f
http://blog.csdn.net/u012099869/article/details/50273507