首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

java:Rest范例(一)(非原创)

2012-10-25 
java:Rest实例(一)(非原创)package firstStepsimport org.restlet.Componentimport org.restlet.Restlet

java:Rest实例(一)(非原创)
package firstSteps;

import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;


public class FirstStepsMain {

   
    public static void main(String[] args) {
        try{
            Component component = new Component();
            component.getServers().add(Protocol.HTTP, 8182);
           
            Restlet restlet = new Restlet(){
                @Override
                public void handle(Request request,Response response){
                     String message = "Resource URI  : " + request.getResourceRef()
                                     + '\n' + "Root URI      : " + request.getRootRef()
                                     + '\n' + "Routed part   : "
                                     + request.getResourceRef().getBaseRef() + '\n'
                                     + "Remaining part: "
                                     + request.getResourceRef().getRemainingPart();
                             response.setEntity(message, MediaType.TEXT_PLAIN);
                }
            };
          
            component.getDefaultHost().attach("/trace",restlet);
            component.start();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}

输入:http://localhost:8182/trace/abc/sofe/pwetewp/wetew%EF%BC%9Flifag
结果:
  

Resource URI  : http://localhost:8182/trace/abc/sofe/pwetewp/wetew%EF%BC%9Flifag
Root URI      : http://localhost:8182/trace
Routed part   : http://localhost:8182/trace
Remaining part: /abc/sofe/pwetewp/wetew%EF%BC%9Flifag

热点排行