有个需求:对同一个接口,我们可以用GET与POST两种方式来请求,这时候就要做兼容了,本例POST使用传json方式,json包用的阿里的fastjson。
@GetMapping("/**") public void list(HttpServletRequest request) { System.out.println("GET"); Enumeration enu=request.getParameterNames(); while(enu.hasMoreElements()){ String paraName=(String)enu.nextElement(); System.out.println(paraName+": "+request.getParameter(paraName)); } return null; } @PostMapping("/**") public void list(@RequestBody JSONObject jsonObject) { System.out.println("无序遍历结果:"); JSONObject jsonObj = JSON.parseObject(jsonObject.toJSONString()); for (Map.Entry<String, Object> entry : jsonObj.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println("-------------------"); System.out.println("有序遍历结果:"); LinkedHashMap<String, String> jsonMap = JSON.parseObject(jsonObject.toJSONString(), new TypeReference<LinkedHashMap<String, String>>() { }); for (Map.Entry<String, String> entry : jsonMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } }