REST实战:SeverClient项目+RESTful理论

  理解一个新的技术,无疑就是使用它了,下面我们就通过一个可执行的demo来展现REST的原理和使用。

  一 Demo

  1.1 服务器端

  1 主程序MainServer.java负责启动一个REST服务组件,并设置端口号,创建主路径/threshold及子路径(见步骤2),最后start启动即可。

public class MainServer extends Application {

    public static void main(String[] args) throws Exception {

        Component com = new Component();
        com.getServers().add(Protocol.HTTP, 12345);
        com.getDefaultHost().attach("/threshold", new ThresholdApplication());
        com.start();
    }
}

  2ThresholdApplicatio.java文件,增添路由, /threshold路径下新增三个路径,/threshold/register /threshold/metricRegister 和 /threshold/info

public class ThresholdApplication extends WadlApplication {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext()) ;
        router.attach("/register" ,RegisterResource.class);
        router.attach("/metricRegister" ,MetricResource.class);
        router.attach("/info" ,InfoResource.class);
        return router;
    }
}

  3InfoResource.java文件,为路径/threshold/register实现get和post接口,post方式需要解读参数。

public class InfoResource extends WadlServerResource{
    @Get
    public Representation method1() throws ResourceException {
        return new StringRepresentation("Info Register Success! Get Method");
    }

    @Post
    public Representation method2(Representation entity)  
            throws ResourceException {
        String result = getRequest().getEntityAsText();
        System.out.println(result);
        return new StringRepresentation("Info Register Success! Post Method");
    }
}

  4MetricResourc.java文件,原理同步骤3

public class MetricResource extends WadlServerResource{
    @Get
    public Representation method1() throws ResourceException {
        return new StringRepresentation("Metric Register Success! Get Method");
    }

    @Post
    public Representation method2(Representation entity)  
            throws ResourceException {
        String result = getRequest().getEntityAsText();
        System.out.println(result);
        return new StringRepresentation("Metric Register Success! Post Method");
    }
}

  5原理同步骤3

public class RegisterResource extends WadlServerResource{
    @Get
    public Representation method1() throws ResourceException {
        return new StringRepresentation("Register Success! Get Method");
    }

    @Post
    public Representation method2(Representation entity)  
            throws ResourceException {
        String result = getRequest().getEntityAsText();
        System.out.println(result);
        return new StringRepresentation("Register Success! Post Method");
    }
}

  1.2 客户端

  测试程序TestClient.java,扮演客户端的角色,请求服务器url.

public class TestClient {

    @Test
    public void test01() throws IOException{
        ClientResource client = new ClientResource("http://localhost:12345/threshold/register");
        Representation result =  client.get() ;        
        System.out.println(result.getText());  
    }
    
    @Test
    public void test02() throws IOException{
        ClientResource client = new ClientResource("http://localhost:12345/threshold/register"); 
        Gson gson = new Gson();
        String str =gson.toJson("1");
        Representation result =  client.post(str) ;    
        System.out.println(result.getText());  
    }
    @Test
    public void test03() throws IOException{
        ClientResource client = new ClientResource("http://localhost:12345/threshold/metricRegister");
        Representation result =  client.get() ;        
        System.out.println(result.getText());  
    }
    
    @Test
    public void test04() throws IOException{
        ClientResource client = new ClientResource("http://localhost:12345/threshold/metricRegister"); 
        Gson gson = new Gson();
        String str =gson.toJson("1");
        Representation result =  client.post(str) ;    
        System.out.println(result.getText());  
    }
    @Test
    public void test05() throws IOException{
        ClientResource client = new ClientResource("http://localhost:12345/threshold/info");
        Representation result =  client.get() ;        
        System.out.println(result.getText());  
    }
    
    @Test
    public void test06() throws IOException{
        ClientResource client = new ClientResource("http://localhost:12345/threshold/info"); 
        Gson gson = new Gson();
        String str =gson.toJson("1");
        Representation result =  client.post(str) ;    
        System.out.println(result.getText());  
    }
    
}

  1.3测试和结果

  1先启动服务器程序,执行main文件

  2执行客户端请求,单元测试程序

  get方式的单元测试1的结果如下:

  备注:项目源码,后期本人会放在github上,https://github.com/rongyux/REST-Demo

 

  二 RESTful 理论

  2.1 论文理论

  什么是REST?首先在其缩写上理解,REpresentation State Transfer ,即(资源)表现层状态转化。最初是2000年Roy Thomas Fielding在他的博士论文中提出的,需要深入理解拜读下其论文。

  下面要理解这三个单词是什么意思。其它几个概念辅助理解这三个概念叙述。

  1 资源:我们平常上网访问的一张图片、一个文档、一个视频等都是资源。

  2 表现层:,一个图片他可以jpg、png等方式展现;就是一个资源可以多种形式表现出来。

  3 URI: 一个URI定位一个资源

  4 状态转化:客户端通过四个HTTP动词,对服务器端资源进行操作,实现"表现层状态转化"。

  就是HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源。

  2.2 REST是一种架构风格  

  

  REST是一种框架规范,和HTTP协议一样,规范统一了表达格式。RESTful架构有一些典型的设计误区。

  最常见的一种设计错误,就是URI包含动词。因为"资源"表示一种实体,所以应该是名词,URI不应该有动词,动词应该放在HTTP协议中。

  举例来说,

  1某个URI是/posts/show/1,其中show是动词,这个URI就设计错了,正确的写法应该是/posts/1,然后用GET方法表示show。

  如果某些动作是HTTP动词表示不了的,你就应该把动作做成一种资源。比如网上汇款,从账户1向账户2汇款500元,错误的URI是:

  POST /accounts/1/transfer/500/to/2

  正确的写法是把动词transfer改成名词transaction,资源不能是动词,但是可以是一种服务:

  POST /transaction HTTP/1.1
  Host: 127.0.0.1
  
  from=1&to=2&amount=500.00

  2另一个设计误区,就是在URI中加入版本号

  http://www.example.com/app/1.0/foo

  http://www.example.com/app/1.1/foo

  http://www.example.com/app/2.0/foo

  因为不同的版本,可以理解成同一种资源的不同表现形式,所以应该采用同一个URI。版本号可以在HTTP请求头信息的Accept字段中进行区分:

  Accept: vnd.example-com.foo+json; version=1.0

  Accept: vnd.example-com.foo+json; version=1.1

  Accept: vnd.example-com.foo+json; version=2.0

  3.3 总结

  1REST四个基本原则:
  1).使用HTTP动词:GET POST PUT DELETE;
  2).无状态连接,服务器端不应保存过多上下文状态,即每个请求都是独立的;
  3).为每个资源设置URI;
  4).通过XML JSON进行数据传递;


  2实现上述原则的架构即可称为RESTFul架构。
  1).互联网环境下,任何应用的架构和API可以被快速理解;
  2).分布式环境下,任何请求都可以被发送到任意服务器;
  3).异构环境下,任何资源的访问和使用方式都统一;

 

 

  源码上传本人github:

  https://github.com/rongyux/REST_ServerClient_Demo

 

 

 

 

  理论部分参考:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/08.3.md

          http://www.ruanyifeng.com/blog/2011/09/restful.html

 

转载于:https://www.cnblogs.com/rongyux/p/5685611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值