Jetty can't locate REST classes when application is packed to jar

时间:2016-03-04 16:00:51

标签: maven jetty embedded-jetty maven-assembly-plugin jar-with-dependencies

When I start my application with embeded Jetty server directly in Eclipse, I can access my REST APIs e.g. on "localhost:8082/getModules" and all is working. However if I create jar-with-dependencies from my application, after I navigate to "localhost:8082/getModules" I get

HTTP ERROR: 404 Problem accessing /database/getModules Reason: Not Found

I found log in console, that is displayed when GET request come and it says, that servlet container can't be found and after that response is set to HTTP 404 not found:

org.glassfish.jersey.servlet.ServletContainer-3e755a96@ca8ad067==org.glassfish.jersey.servlet.ServletContainer,-1,true

I think problem is somewhere in getJersey() method:

public void init() throws Exception {
    server = new Server(8082);
    final HandlerList handlers = new HandlerList();
    handlers.addHandler(getJersey()); 
    server.setHandler(handlers);
    server.start();
}

private Handler getJersey() {
    ServletContextHandler context = 
        new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    ProtectionDomain domain = this.getClass().getProtectionDomain();
    String location = domain.getCodeSource().getLocation().toString();
    context.setResourceBase(location);

    final ResourceConfig resourceConfig = new ResourceConfig().
        register(GensonCustomResolver.class).packages(true,
        new String[] {"org.iot.gateway.modules.DatabaseWebInterfaceModule.rest"});
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    context.addServlet(new ServletHolder(servletContainer), "/*");     
    return context;
}

or maybe problem is somewhere in 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>IoT</groupId>
  <artifactId>DatabaseWebInterfaceModule</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>DatabaseWebInterfaceModule</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

  <dependencies>

    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections-maven</artifactId>
        <version>0.9.9-RC2</version>
    </dependency>  
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-metainf-services</artifactId>
        <version>2.16</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
    <dependency>
        <groupId>com.owlike</groupId>
        <artifactId>genson</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>de.ruedigermoeller</groupId>
        <artifactId>fst</artifactId>
        <version>2.24</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-servlet</artifactId>
      <version>1.13</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
        <groupId>IoT</groupId>
        <artifactId>Modules</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlets</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-util</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-security</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-io</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-http</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-continuation</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-annotations</artifactId>
        <version>9.2.9.v20150224</version>
    </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.16</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.16</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.16</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.4</version>
    </dependency>
  </dependencies>
</project>

So how could I correctly set link to package with my rest classes?

1 个答案:

答案 0 :(得分:1)

我解决了!

在我的getJersey()方法中,我更换了

final ResourceConfig resourceConfig = new ResourceConfig().
    register(GensonCustomResolver.class).packages(true,
    new String[] {"org.iot.gateway.modules.DatabaseWebInterfaceModule.rest"});

    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.register(org.iot.gateway.modules.DatabaseWebInterfaceModule.rest.RestTest.class);
    resourceConfig.register(org.iot.gateway.modules.DatabaseWebInterfaceModule.rest.WebRequestHandler.class);

所以我没有加载包含我的REST类的整个包,而是添加了每个单独处理我的REST请求的类