Restlet(1.2-m2)へ入門してみんとす
あるWebサービスから別個稼働しているWebサービスのデータを表示したくて、RESTで解決できないかなあということで、検証がてらRestletを勉強してみます。
(私の現状でのRESTへの知識:HTTPリクエストに対してxmlが帰ってくるから適当にパーサして使ってね!)
下準備
ひとまず環境を作ります。
FirstStepsをなぞってみる
公式サイトのFirstStepをなぞってみます。パッケージ名はrestlet.testingにしました。
HelloWorldResourceの作成
package restlet.testing; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; public class HelloWorldResource extends ServerResource { @Get public String represent() { return "Hello, world."; } }
HelloWorldApplicationの作成(公式だとFirstStepsApplicationですね)
package restlet.testing; import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; public class HelloWorldApplication extends Application { @Override public Restlet createRoot() { Router router = new Router(getContext()); router.attachDefault(HelloWorldResource.class); return router; } }
web.xmlの作成
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Testing Rest Application</display-name> <description> This is only a test of the Restlet. </description> <context-param> <param-name>org.restlet.application</param-name> <param-value>restlet.testing.HelloWorldApplication</param-value> </context-param> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
context.xmlの作成
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Testing Rest Application</display-name> <description> This is only a test of the Restlet. </description> <context-param> <param-name>org.restlet.application</param-name> <param-value>restlet.testing.HelloWorldApplication</param-value> </context-param> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>