关于SpringMVC中json数据request获取不到和bean获取为null
2017-03-20 00:00
465 查看
1、通过注解获取
@RequestMapping(value = "/datatech/customer/presenceUmByAgentNo", method = RequestMethod.POST)
public @ResponseBody
Map<String, String> presenceUmByAgentNo(@RequestBody Map map) {
System.err.println(map);
return map;
}
通过以上的方式json类型会直接被转换为map,然后通过一般的map的操作就可以进行相关的处理了。
原理分析:
JSON被转成了LinkedHashMap只能用Map或者对象接收
2、通过流的方式获取
* springMVC中参数类型配置为json则获取参数的方式不能够为request的方式,必须以流的方式进行读取,并将获取到的json类型转换为对应的Map或则会dto类型
* private String parseRequestParamsFromRequestBody(HttpServletRequest request) throws IOException{
StringBuffer sb = new StringBuffer();+
String params = null;
BufferedReader reader = request.getReader();
while(null != (params = reader.readLine())){
sb.append(params);
}
return sb.toString();
}
---------------------------------------------------------------------------------
* springMVC注解获得的bean为null,问题来源:没有在配置文件中将对应的注解如@BizServicesProxy
@RequestMapping(value = "/datatech/customer/presenceUmByAgentNo", method = RequestMethod.POST)
public @ResponseBody
Map<String, String> presenceUmByAgentNo(@RequestBody Map map) {
System.err.println(map);
return map;
}
通过以上的方式json类型会直接被转换为map,然后通过一般的map的操作就可以进行相关的处理了。
原理分析:
JSON被转成了LinkedHashMap只能用Map或者对象接收
2、通过流的方式获取
* springMVC中参数类型配置为json则获取参数的方式不能够为request的方式,必须以流的方式进行读取,并将获取到的json类型转换为对应的Map或则会dto类型
* private String parseRequestParamsFromRequestBody(HttpServletRequest request) throws IOException{
StringBuffer sb = new StringBuffer();+
String params = null;
BufferedReader reader = request.getReader();
while(null != (params = reader.readLine())){
sb.append(params);
}
return sb.toString();
}
---------------------------------------------------------------------------------
* springMVC注解获得的bean为null,问题来源:没有在配置文件中将对应的注解如@BizServicesProxy
相关文章推荐
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- 使用postman请求springmvc接口时,request获取不到数据
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- 关于springmvc 返回json数据null字段的显示问题
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- springMVC架构后台获取不到json数据的问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- kindeditor上传文件在SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题 .
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法