我有一个简单的Restlet应用程序,当它作为一个来自Eclipse的J2SE应用程序运行时工作正常,但是当打包为战争并使用" mvn jetty运行时:run-war" (jetty-maven-plugin),Application.createInboundRoot方法不会被调用,因此没有配置路由。
相关的错误消息似乎是
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 jetty从命令行运行战争:run-war"。
点击网址进行测试:
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 o.e.j.m.p.JettyWebAppContext@6307c51a{/,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 ServerConnector@459c745a{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: com.merck.modspace.rest.ModspaceRestletApplication@74e1d431 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("/users/{user}", UserResource.class);
router.attach("/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();
}
}
答案 0 :(得分:2)
这现在有效。我认为这个问题是由于:
<dependency>
<groupId>org.restlet</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>2.0-M3</version>
</dependency>
应该是:
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>2.2.0</version>
</dependency>
注意groupID错误且版本已旧。