发布于 2015-12-29 23:17:34 | 588 次阅读 | 评论: 0 | 来源: PHPERZ

这里有新鲜出炉的RESTful 教程,程序狗速度看过来!

RESTful

REST(英文:Representational State Transfer,简称REST)描述了一个架构样式的网络系统,比如 web 应用程序。它首次出现在 2000 年 Roy Fielding 的博士论文中,他是 HTTP 规范的主要编写者之一。在目前主流的三种Web服务交互方案中,REST相比于SOAP(Simple Object Access protocol,简单对象访问协议)以及XML-RPC更加简单明了,无论是对URL的处理还是对Payload的编码,REST都倾向于用更加简单轻量的方法设计和实现。值得注意的是REST并没有一个明确的标准,而更像是一种设计的风格。


示例

package com.test.webser;
/** 
* RESTful WebService入门 
* */
/*
import com.sun.jersey.api.container.httpserver.HttpServerFactory; 
import com.sun.net.httpserver.HttpServer;
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; */
import java.io.IOException; 
import java.net.InetAddress;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;
//指定URI 
@Path("/helloworld") 
public class RESTfulHelloWorld { 
        //处理HTTP的GET请求 
        @GET 
        // 处理请求反馈的内容格式为"text/plain" 
        @Produces("text/plain") 
        public String getClichedMessage() { 
         System.out.println("--yes, find this way --" );
                return "Hello World!"; 
        }
        public static void main(String[] args) throws IOException { 
         String ip="";
      String address="";
      
      InetAddress addr = InetAddress.getLocalHost();
      ip=addr.getHostAddress()+"";//获得本机IP
      address=addr.getHostName()+"";//获得本机名称
//      System.out.println( address+"---cow  boy--"+ip);
         
                //创建RESTful WebService服务 
                HttpServer server = HttpServerFactory.create("http://" + ip + ":8383/"); 
                //启动服务,这会导致新开一个线程 
                server.start(); 
                //输出服务的一些提示信息到控制台 
                System.out.println("RESTful WebService服务已经启动"); 
                System.out.println("服务访问地址: http://" + ip + ":8383/helloworld"); 
        } 
}


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务