@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接口示例代码restful接口示例代码restful接口示例代码restful接口示例代码
包含post、delete、put、get方法四个restful支持的服务,客户端向服务端发出的post等四个操作的http头,并携带参数向服务端获取响应。 实现了book的增删改查
Qt http同步请求测试(restfull中常用的get/post/put/delete、文件上传/下载) 示例源码 https://blog.csdn.net/aggs1990/article/details/124104548 CSDN审核可能较慢,如无法下载,可以过段时间再回来看下
RestfulAPI开发示例
本例是从前端,以Ajax的形式向豆瓣RESTFul服务器发起请求的简单示例。
使用Jersey框架实现RESTful访问方式的示例项目,属半成品,尚缺jar包,还有编译错误,请不要下载。
开发RESTful WebService 示例
spring boot restful 接口示例项目,很详细,希望对大家有帮助。
spring-boot-web代码示例,是IDEA的项目工程,使用restful风格实现增删查改,静态数据
学习 RESTful Web 服务的示例 运行示例 克隆并运行示例应用程序 git clone https://github.com/oliverbauer/tapestry-resteasy-hibernate.git cd tapestry-resteasy-hibernate mvn clean jetty:run 从此存储库...
esrestful接口demoesrestful接口demoesrestful接口demoesrestful接口demoesrestful接口demo
post get http://127.0.0.1:5555/restful/rpc { "db":"mycon1","function":"sp_fun_test", "in1":10.1,"in2":"就是这么简单" }
一个实体CRUD Restful API的简单示例。 Express + MongoDB 用于安装package.json依赖项: npm install 与您的集群变更进行连接的DB_CONNECTION在.env文件
Restful Jersey实现 java 参考学习页面 http://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/ 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务
RESTful Web APIs - Richardson, Leonard & Amundsen, Mike & Ruby, Sam.mobi
NestJS示例 NestJS RESTful API示例项目。文件建立安装依赖项。 $ npm install 运行应用# development$ npm run start# watch mode$ npm run start:dev# production mode$ npm run start:prod 测试# unit tests$ npm...
并且支持RESTful API GET、POST、PUT、DELETE、PATCH的请求。同时使用YYCache做了强大的缓存策略。 拥有 AFNetwork 大部分常用功能,包括网络状态监听等,提供类方法和实例方法调用。 非常好的扩展性,开放出...
RESTful-php-示例 用PHP实现的RESTful API的示例
本资源是一个 Wowza 支持 RESTful web 服务的一个简单的 demo。关于如何让 wowza 能够支持 RESTful api 的步骤请参考博客《让你的 wowza 服务器提供 RESTful web 服务》,地址是:...