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