-
Notifications
You must be signed in to change notification settings - Fork 0
Rest.li with Servlet Containers
Rest.li may be run in a variety of http frameworks. Out of the box, Rest.li supports both Netty and Servlet containers like Jetty.
This describes how to run Rest.li one a servlet container by building a war containing a rest.li servlet. It also covers how to run Rest.li with Jetty.
Rest.li provides a RestliServlet class to integrate rest.li into any java servlet container.
To use RestliServlet you will add a web.xml file.
For example, below is example-standalone-app/server/src/main/webapp/WEB-INF/web.xml, an extremely simple web.xml example that creates a RestliServlet and configures it to load all rest.li resources in the com.example.fortune.impl package.
Notice that RestliServlet automatically scans all resource classes in the specified package and initializes the REST endpoints/routes without any hard-coded connection. Adding additional resources or operations can be done simply by expanding your data schema and providing additional functionality in your Resource class(es).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<display-name>Fortunes App</display-name>
<description>Tells fortunes</description>
<!-- servlet definitions -->
<servlet>
<servlet-name>FortunesServlet</servlet-name>
<servlet-class>com.linkedin.restli.server.RestliServlet</servlet-class>
<init-param>
<param-name>resourcePackages</param-name>
<param-value>com.example.fortune.impl</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- servlet mappings -->
<servlet-mapping>
<servlet-name>FortunesServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>The parseq thread count (use to make outbound requests) may optionally be configured by setting the parSeqThreadPoolSize servlet init param, e.g.:
<init-param>
<param-name>parseqThreadPoolSize</param-name>
<param-value>10</param-value>
</init-param>To configure jetty’s inbound request thread pool size, see jetty’s documentation.
To build a war, we need a build.gradle file in the server directory, which should look like this:
apply plugin: 'war'
apply plugin: 'pegasus'
ext.apiProject = project(':api')
dependencies {
compile project(path: ':api', configuration: 'dataTemplate')
compile spec.product.pegasus.restliServer
}For on the gradle ‘war’ plugin, see: Gradle War Plugin
To run Rest.li on Jetty, you may use the Gradle Jetty Plugin . This plugin makes it easy to not only build a war but also run it from gradle. To setup, use a build.gradle like the below.
apply plugin: 'jetty'
apply plugin: 'pegasus'
ext.apiProject = project(':api')
dependencies {
compile project(path: ':api', configuration: 'dataTemplate')
compile spec.product.pegasus.r2Jetty
compile spec.product.pegasus.restliServer
}To start Rest.li on Jetty run:
gradle JettyRunWarThe server will start on port 8080 under the /server context path. E.g.
curli http://localhost:8080/fortunes/1