我有一個簡單的Restlet應用程序,它在從Eclipse運行爲J2SE應用程序時工作正常,但打包爲一場戰爭並以「 mvn jetty:run-war「(jetty-maven-plugin),Application.createInboundRoot方法沒有被調用,因此沒有配置路由。當應用程序作爲Eclipse/J2SE運行但不作爲maven碼頭時調用restlet createInboundRoot:run-war
出現相關的錯誤消息是
WARNING: A filter was executed without a next Restlet attached to it.
可以從控制檯輸出看到,在J2SE情況ModspaceRestletApplication.createInboundRoot被調用並在該方法中不被調用的情況下,J2EE。爲什麼不?
以J2SE運行(Eclipse-> Run As-> Java Application)。
測試通過點擊網址:
http://localhost:8182/modspace/users/fred/entries
形成正確返回響應和控制檯輸出爲:命令行
In ModspaceRestApplication.ctor
Mar 25, 2014 2:41:15 PM org.restlet.engine.http.connector.HttpServerHelper start
INFO: Starting the internal HTTP server on port 8182
In ModspaceRestApplication.createInboundRoot
運行戰爭使用「命令mvn碼頭:運行戰爭」。
測試通過點擊網址:
http://localhost:8080/modspace/users/fred/entries
瀏覽器看到
The server encountered an unexpected condition which prevented it from fulfilling the request
和控制檯輸出爲:
2014-03-25 14:42:27.125:INFO:oejs.Server:main: jetty-9.1.0.M0
2014-03-25 14:42:31.507:INFO:oejsh.ContextHandler:main: Started [email protected]{/,file:/Users/wilma/eclipse-workspace/modspace-rest/target/modspace-rest-0.0.1-SNAPSHOT/,AVAILABLE}{/Users/wilma/eclipse-workspace/modspace-rest/target/modspace-rest-0.0.1-SNAPSHOT.war}
2014-03-25 14:42:31.509:WARN:oejsh.RequestLogHandler:main: !RequestLog
2014-03-25 14:42:32.004:INFO:oejs.ServerConnector:main: Started [email protected]{HTTP/1.1}{0.0.0.0:8080}
[INFO] Started Jetty Server
In ModspaceRestApplication.ctor
2014-03-25 14:42:50.964:INFO:/:qtp721530251-44: RestletServlet: [Restlet] Attaching application: [email protected] to URI: /modspace
Mar 25, 2014 2:42:50 PM org.restlet.routing.Filter doHandle
WARNING: A filter was executed without a next Restlet attached to it.
Mar 25, 2014 2:42:51 PM org.restlet.engine.log.LogFilter afterHandle
INFO: 2014-03-25 14:42:50 0:0:0:0:0:0:0:1 - 0:0:0:0:0:0:0:1 8080 GET /modspace/users/fred/entries - 500 365 - 20 http://localhost:8080 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36 -
工具:
Apache Maven 3.0.5
Java version: 1.7.0_51
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="ModspaceWS" 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>Modspace RESTlet web service</display-name>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<!-- Application class name -->
<param-name>org.restlet.application</param-name>
<param-value>com.merck.modspace.rest.ModspaceRestletApplication</param-value>
</init-param>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/modspace/*</url-pattern>
</servlet-mapping>
</web-app>
的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.merck.modspace.rest</groupId>
<artifactId>modspace-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ModSpaceWebService</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Restlet framework repository</name>
<url>http://maven.restlet.org</url>
</repository>
<repository>
<id>mvnrepository</id>
<url>https://mvnrepository.com//</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.restlet</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>2.0-M3</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.1.0.M0</version>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>
的Restlet應用類:
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
public class ModspaceRestletApplication extends Application {
Router router;
public ModspaceRestletApplication() {
System.out.println("In ModspaceRestApplication.ctor");
//this.setInboundRoot(this.createInboundRoot());
}
@Override
public synchronized Restlet createInboundRoot() {
// Create a root router
router = new Router(getContext());
System.out.println("In ModspaceRestApplication.createInboundRoot");
// Attach the handlers to the root router
router.attach("https://stackoverflow.com/users/{user}", UserResource.class);
router.attach("https://stackoverflow.com/users/{user}/entries", ModelResource.class);
return router;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Create a new Restlet component and add a HTTP server connector to it
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
// Then attach it to the local host
component.getDefaultHost().attach("/modspace", new ModspaceRestletApplication());
// Now, let's start the component!
// Note that the HTTP server connector is also automatically started.
component.start();
}
}