项目结构如下:
1、创建两个继承 ServerResource的类(FirstServerResource.java、SecondServerResource.java):
FirstServerResource.java:
SecondServerResource.java:
2、创建两个继承 Application 的类(FirstServerApplication.java、SecondServerApplication.java):
FirstServerApplication.java:
SecondServerApplication.java:
3、创建一个继承 Component 的类(MyComponent.java):
MyComponent.java:
(1).
package server; import org.restlet.Component; import org.restlet.data.Protocol; import component.MyComponent; public class RestletServer { /** * 将Restlet服务当做单独的Java程序进行部署 */ public static void main(String[] args) throws Exception { Component component = new MyComponent(); component.getServers().add(Protocol.HTTP, 8080); component.start(); System.out.println("WebServer服务启动成功!开启端口号为:8080"); } }
(2).
package server; import org.restlet.Component; import org.restlet.data.Protocol; import application.FirstServerApplication; public class RestletServer { /** * 将Restlet服务当做单独的Java程序进行部署 */ public static void main(String[] args) throws Exception { Component component = new Component(); component.getServers().add(Protocol.HTTP, 8080); component.getDefaultHost().attach("/testRestLet01-3/first",new FirstServerApplication()); component.getDefaultHost().attach("/testRestLet01-3/second",new FirstServerApplication()); System.out.println("WebServer服务启动成功!开启端口号为:8080"); } }
5.启动RestletServer.java程序,即将Restlet服务当做单独的Java程序进行部署,然后通过浏览器访问:
如果用的是4.(1)中的写法,访问地址如下:http://localhost:8182/first、http://localhost:8182/second
如果用的是4.(2)中的写法,访问地址如下:http://localhost:8182/testRestLet01-2/first、http://localhost:8182/testRestLet01-2/second