restful RequestMapping POST\PUT 示例&要点
程序员文章站
2022-07-12 18:51:20
...
@RequestMapping(value = "/xxx/xxxxxx", method = RequestMethod.POST) public Response fun1(HttpServletRequest request, @PathVariable("paraA") String a, @RequestParam("ParaB") String b, @RequestParam("ParaC") String c ) { //code here return response; } @RequestMapping(value = "/xxx/yyyyyyy", method = RequestMethod.PUT) public Response fun2(HttpServletRequest request, @PathVariable("paraA") String a, @RequestParam("ParaB") String b, @RequestParam("ParaC") String c ) { //code here return response; }
要点:
@RequestParam
A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;
B)用来处理Content-Type: 为 application/x-www-form-urlencoded
编码的内容,提交方式GET、POST;
C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;
*如果是json,则要以下写法:
@RequestMapping(value = "/api/xxxx/{param}", method = { RequestMethod.PUT }) public Response fun3( @PathVariable(value = "param") String param, HttpServletRequest request) { logger.info("============= API start =================="); //code service here logger.info("============= API end =================="); return response; }
要用上HttpServletRequest,然后在service层,调用
JSONObject json = ParametersUtil.getRequestBodyWithJson(param,request);
ParametersUtil:
public class ParametersUtil { private static final Logger logger = LoggerFactory .getLogger(ParametersUtil.class); public static JSONObject getRequestBodyWithJson(String parameter, HttpServletRequest request) { try { BufferedReader br = new BufferedReader(new InputStreamReader( request.getInputStream())); String line = null; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); } logger.info("Request Body:" + sb.toString()); String reqBody = URLDecoder.decode(sb.toString(), "UTF-8"); JSONObject json = new JSONObject(reqBody); logger.info( "[getRequestBodyWithJson][{}]-- get request body with json successfully", parameter); return json; } catch (Exception e) { logger.error( "[getRequestBodyWithJson][{}]-- get request body with json fail, exception is [{}]", parameter, e.getMessage()); return null; } } public static JSONArray getJsonArray(JSONObject jsonObject, String key) { try { if (jsonObject == null) { return null; } else { return jsonObject.getJSONArray(key); } } catch (Exception e) { return null; } } public static String covertJSONArray2String(JSONArray jsonArray) { if (jsonArray == null || jsonArray.length() == 0) { return null; } List<String> list = new ArrayList<String>(); for (int i = 0; i < jsonArray.length(); i++) { String s = (String) jsonArray.get(i); if (s != null && !s.isEmpty()) { list.add(s.trim()); } } if (list.isEmpty()) { return null; } else { return list.toString().replace("[", "").replace("]", "") .replace(" ", ""); } } public static String getJsonValue(JSONObject jsonObject, String key) { try { if (jsonObject == null) { return null; } else { return jsonObject.getString(key); } } catch (Exception e) { return null; } } public static JSONObject getJsonObject(JSONObject jsonObject, String key) { try { if (jsonObject == null) { return null; } else { return jsonObject.getJSONObject(key); } } catch (Exception e) { return null; } } }
上一篇: 修改第三方源码并重新打包
推荐阅读
-
在Restful风格中,axios请求中, method:“put“/‘delete‘/‘get‘/‘post‘四种方式
-
jQuery调用RESTful WCF示例代码(GET方法/POST方法)
-
restful RequestMapping POST\PUT 示例&要点
-
restful-api的put方法和post方法区别
-
restful-api的put方法和post方法区别
-
jQuery调用RESTful WCF示例代码(GET方法/POST方法)_jquery
-
jQuery调用RESTful WCF示例代码(GET方法/POST方法)_jquery
-
ajax请求基于restFul的WebApi(post、get、delete、put)
-
jQuery调用RESTful WCF示例代码(GET方法/POST方法)
-
在Restful风格中,axios请求中, method:“put“/‘delete‘/‘get‘/‘post‘四种方式