REST,HTTP POST demo
来源:互联网 发布:初学者mysql书籍推荐 编辑:程序博客网 时间:2024/05/14 02:33
客户端程序(其中,content为要传输的json数据):
HttpPost httppost = new HttpPost("http://xxxhost/api/my_path?mc=1");HttpClient httpclient = HttpClientBuilder.create().build();try {StringEntity reqEntity = new StringEntity(<strong><span style="color:#ff0000;">content</span></strong>);reqEntity.setContentType("application/json");httppost.setEntity(reqEntity);HttpResponse httpresponse = httpclient.execute(httppost);HttpEntity entity = httpresponse.getEntity();String body = EntityUtils.toString(entity);System.out.println(body);
服务器端程序:
@Path("/<span style="font-family: Arial, Helvetica, sans-serif;">my_path</span>")public class Events {@POST@Consumes(MediaType.APPLICATION_JSON)public Response addEvent(<strong><span style="color:#ff0000;">@QueryParam</span></strong>(ConfigItemConstants.CONFIG_ITEM_MC) String mc, Object eventStr){if(!GenericValidator.isNullOrBlank(mc) && !ConfigItemUtil.verifyMC(mc)){//在这里,验证你传过来的mc参数,通过则继续return ResponseUtil.setErrorResponse(403, "Forbidden", "User does not have access to this service!");}EventService eventService = new EventService();int code = 200;String resBody = "";int rtn = 0;try {Long id = eventService.createEvent(eventStr);//传过来的json数据都在eventStr中,你可以根据需求自己处理,比如存储到NoSQL中。rtn = 1;} catch (Exception e) {rtn = -1;e.printStackTrace();} if(rtn == 1){code = 201;resBody = "The event created successfully";}else if(rtn == -1){code = 400;resBody = "Format of request is not correct";}//logger.exit();if(rtn == 1){return ResponseUtil.setResponse(code, resBody);} else {return ResponseUtil.setErrorResponse(code, "Bad Request", resBody);}}@Path("{delete}")@DELETE@Consumes(MediaType.APPLICATION_JSON)public Response deleteEvent(@QueryParam(ConfigItemConstants.CONFIG_ITEM_MC) String mc, String vehicleId){if(!GenericValidator.isNullOrBlank(mc) && !ConfigItemUtil.verifyMC(mc)){return ResponseUtil.setErrorResponse(403, "Forbidden", "User does not have access to this service!");}EventService eventService = new EventService();int code = 200;String resBody = "";int rtn = 0;try{Long id = eventService.deleteEvent(vehicleId);rtn = 1;}catch(Exception e){rtn = -1;e.printStackTrace();}if(rtn == 1){code = 201;resBody = "The event created successfully";}else if(rtn == -1){code = 400;resBody = "Format of request is not correct";}if(rtn == 1){return ResponseUtil.setResponse(code, resBody);} else {return ResponseUtil.setErrorResponse(code, "Bad Request", resBody);}}}
Note:客户端程序和服务器端程序只是相对来说的,这是受传统开发web project的思维影响!新的技术都可能颠覆这种思维!做技术,思维一定要跟上!尤其是出现了REST,DevOps,cloud service,Nodejs,distribution app。
准确来说一个是http post request端,一个是http post response端。实际上,这两个都是服务器容器App。
0 0
- REST,HTTP POST demo
- Http Post Get Demo
- REST HTTP中的POST PUT GET
- Android Http Get/Post 传递参数 Demo
- NodeJS HTTP模块编写POST提交表单Demo
- [REST Jersey] @QueryParam Demo
- rest 应用demo
- rest 应用demo 2
- REST应该放弃使用http头GET、POST、PUT和DELETE来表达操作
- Angular 4.3 HttpClient (Angular访问 REST Web 服务) 二、Http POST
- http post
- http post
- http-post
- http post
- http post
- HTTP-POST
- MyEclipse开发Rest服务Demo
- Simple REST Client POST使用方法
- 游戏任务剧情布局系统分析
- R语言教程(二)
- 面向对象
- java语言实现队列
- 黑马程序员——Java 基础 面向对象之封装 (复习)
- REST,HTTP POST demo
- 关于Azure Storage 的一点研究
- PAT 1065. A+B and C (64bit) (20)
- Android通知栏颜色改变方法
- 一个非常正确且强大的编程学习方法
- 如何使gridView中的每条记录分两行显示
- 用Mathematica拯救一只数字化猫(1)
- HBase之Memstore刷写
- CDH5.4 hadoop 修改日志默认路径