使用Tomcat部署RestletService很简单。 只需要配置好web.xml就行了。 其余代码不用做改动。
两个Resource
public class AccountResource extends ServerResource{
@Get
public Representation getAccountInfo() throws IOException{
return new StringRepresentation("Account Info") ;
}
}
public class MovieResource extends ServerResource{
@Get
public Representation getMovieInfo() throws IOException{
Movie movie = new Movie() ;
movie.setName("速度与激情6");
movie.setSize(100000l);
movie.setMinutes(120);
//将movie对象包装成为一个JaxbRepresentation并返回
JaxbRepresentation<Movie> result = new JaxbRepresentation<Movie>(movie);
return result ;
}
}
两个Application
public class AccountApplication extends Application{
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext()) ;
router.attach("/info" , AccountResource.class);
return router ;
}
}
public class MovieApplication extends Application{
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext()) ;
router.attach("/info" , MovieResource.class);
return router ;
}
}
一个Component
public class MovieComponent extends Component{
public MovieComponent(){
setName("movie web service");
setDescription("movie web service");
setOwner("zhoufeng");
setAuthor("zhoufeng");
getDefaultHost().attach("/movie" , new MovieApplication());
getDefaultHost().attach("/account" ,new AccountApplication());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>restlettest-project01</display-name>
<servlet>
<servlet-name>MovieServerComponent</servlet-name>
<servlet-class>
org.restlet.ext.servlet.ServerServlet
</servlet-class>
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>com.zf.restlet.demo13.server.MovieComponent</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MovieServerComponent</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
启动Tomcat。
然后就可以访问http://localhost:8080/restlettest-project02/movie/info 与 http://localhost:8080/restlettest-project02/account/info 就能够看到两个Resource的输出了