restlet2.1 学习笔记(二) 分别处理Get Post Put请求

本文介绍Restlet框架相较于Servlet的优势,不仅支持GET与POST请求,还支持Delete、Put、OPTIONS等HTTP方法。通过实例演示如何使用Restlet定义资源类并实现不同HTTP方法的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

servlet只支持GET与POST两种请求。

但是restlet除了支持GET与POST请求外还支持Delete Put OPTIONS 等多种请求 。


第一步,编写资源类

(可以将资源类想象成Struts2的Action ,每个加上注解的方法都是一个ActionMethod)

MovieResource.java

[java] view plain copy
  1. packagecom.zf.restlet.demo02.server;
  2. importorg.restlet.resource.Delete;
  3. importorg.restlet.resource.Get;
  4. importorg.restlet.resource.Post;
  5. importorg.restlet.resource.Put;
  6. importorg.restlet.resource.ServerResource;
  7. /**
  8. *以3中Method为例
  9. *@authorzhoufeng
  10. *
  11. */
  12. publicclassMovieResourceextendsServerResource{
  13. @Get
  14. publicStringplay(){
  15. return"电影正在播放...";
  16. }
  17. @Post
  18. publicStringpause(){
  19. return"电影暂停...";
  20. }
  21. @Put
  22. publicStringupload(){
  23. return"电影正在上传...";
  24. }
  25. @Delete
  26. publicStringdeleteMovie(){
  27. return"删除电影...";
  28. }
  29. }

第二步,使用html客户端访问(html默认只支持get与post访问。所以下面演示着两种)

demo02.html

[html] view plain copy
  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <metacharset="UTF-8">
  5. <title>demo02</title>
  6. </head>
  7. <body>
  8. <formaction="http://localhost:8888/"method="get"target="_blank">
  9. <inputtype="submit"value="Get请求"/>
  10. </form>
  11. <formaction="http://localhost:8888/"method="post"target="_blank">
  12. <inputtype="submit"value="Post请求"/>
  13. </form>
  14. </body>
  15. </html>

访问该html通过两个按钮可以发送不同的请求,并会有不同的返回值



第三步:使用Restlet编写客户端调用

MovieClient.java

[java] view plain copy
  1. packagecom.zf.restlet.demo02.client;
  2. importjava.io.IOException;
  3. importorg.junit.Test;
  4. importorg.restlet.representation.Representation;
  5. importorg.restlet.resource.ClientResource;
  6. publicclassMovieClient{
  7. @Test
  8. publicvoidtest01()throwsIOException{
  9. ClientResourceclient=newClientResource("http://localhost:8888/");
  10. Representationresult=client.get();//调用get方法
  11. System.out.println(result.getText());
  12. }
  13. @Test
  14. publicvoidtest02()throwsIOException{
  15. ClientResourceclient=newClientResource("http://localhost:8888/");
  16. Representationresult=client.post(null);//调用post方法
  17. System.out.println(result.getText());
  18. }
  19. @Test
  20. publicvoidtest03()throwsIOException{
  21. ClientResourceclient=newClientResource("http://localhost:8888/");
  22. Representationresult=client.put(null);//调用put方法
  23. System.out.println(result.getText());
  24. }
  25. @Test
  26. publicvoidtest04()throwsIOException{
  27. ClientResourceclient=newClientResource("http://localhost:8888/");
  28. Representationresult=client.delete();//调用delete方法
  29. System.out.println(result.getText());
  30. }
  31. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值