restful RequestMapping POST\PUT 示例&要点

网友投稿 586 2022-09-06

restful RequestMapping POST\PUT 示例&要点

restful RequestMapping POST\PUT 示例&要点

@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-该注解有两个属性: 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 list = new ArrayList(); 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; } }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:windows系统hung住收集日志方案
下一篇:phpMyAdmin批量修改Mysql数据表前缀的方法(php修改mysql指定表数据)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~