很尴尬的一个错误,看程序:
controller代码:
@RequestMapping(value = "/publish",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8") @ResponseBody
public String paramTest(@RequestBody String param) throws Exception { param = URLDecoder.decode(param, "utf-8"); JSONObject jsonObject = JSONObject.parseObject(param); Map<String, Object> map = jsonObject.getInnerMap(); //title //content String newsTitle = (String) map.get("title"); String newsContent = (String) map.get("content"); String fileTitle;
//这是已经正确的解决办法 int newsType = Integer.valueOf((String)map.get("newsType")) ;
int temp = newsContent.hashCode(); if (0 > temp) { fileTitle = 1 + String.valueOf(temp); } else { fileTitle = String.valueOf(temp); } System.out.println(newsTitle +" "+ newsType +" "+ fileTitle +" " + newsContent); return "ok"; }
请求参数是这样的:
{"title":"12345678","newsType":"1","content":" </p>\n "}
如果是Map<String,String>是不是就没有这个错误了?但是参数有时候就是integer的呢是不是又会报错了呢?