泽西Guice Servlet通过Maven Tomcat - 404

时间:2018-04-10 20:24:00

标签: java maven jersey guice

好的,所以我无法让JerseyGuice的HelloWorld在我的生命中工作。 我确信我错过了一些简单而愚蠢的东西,所以我只想把所有这些都丢掉:

它应该通过Maven命令行运行,如: .ssh

应该回复:http://localhost/great/stuff

pom.xml :(运行配置文件位于最底层)

mvn tomcat7:run

的web.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0">
<groupId>HelloWorldFromJerseyGuice</groupId>
<artifactId>HelloWorldFromJerseyGuice</artifactId>
version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <common.core.lib.version>0.15.0</common.core.lib.version>
    <common.codec.version>1.6</common.codec.version>
    <http.lib.version>0.17.0</http.lib.version>
    <jackson.version>2.6.0</jackson.version>
    <jersey.version>1.19</jersey.version>
    <logback.version>3.0.0</logback.version>
    <tomcat.port>8889</tomcat.port>
    <xmlJaxbVersion>2.2.3</xmlJaxbVersion>
    <ribbon.version>0.3.13</ribbon.version>
    <context.root>helloworldservice</context.root>
    <swagger.version>0.54.0</swagger.version>
    <skipITs>true</skipITs>
</properties>

<dependencies>

    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-bundle -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-guice -->
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-guice</artifactId>
        <version>1.19.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
    </dependency>

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.8.1</version>
    </dependency>

    <!-- Use Guava for common utilities -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>15.0</version>
    </dependency>
    <!-- This dependency is required for tomcat7:run directives via Maven -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.4</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.ow2.asm</groupId>
        <artifactId>asm</artifactId>
        <version>5.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.ow2.asm</groupId>
        <artifactId>asm-all</artifactId>
        <version>5.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xbean</groupId>
        <artifactId>xbean-finder</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xbean</groupId>
        <artifactId>xbean-bundleutils</artifactId>
        <version>3.17</version>
    </dependency>

    <!-- Use JUnit for unit and integration tests -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- Use Mockito for mock object support in unit tests -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>3.0.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.11</version>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>${xmlJaxbVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics-runtime</artifactId>
        <version>0.6.3</version>
    </dependency>

    <!-- Apache commons -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${common.codec.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-compress</artifactId>
        <version>1.5</version>
    </dependency>

    <dependency>
        <groupId>com.netflix.ribbon</groupId>
        <artifactId>ribbon-core</artifactId>
        <version>${ribbon.version}</version>
        <exclusions>
            <exclusion>
                <groupId>com.netflix.rxjava</groupId>
                <artifactId>rxjava-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.netflix.ribbon</groupId>
        <artifactId>ribbon-httpclient</artifactId>
        <version>${ribbon.version}</version>
    </dependency>

    <!-- rxjava -->
    <dependency>
        <groupId>io.reactivex</groupId>
        <artifactId>rxjava</artifactId>
        <version>1.1.0</version>
    </dependency>

    <!-- apache cxf -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-api</artifactId>
        <version>2.7.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-ws-security</artifactId>
        <version>2.7.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>2.7.3</version>
        <exclusions>
            <exclusion>
                <groupId>asm</groupId>
                <artifactId>asm</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>2.7.3</version>
    </dependency>

</dependencies>

<profiles>
    <profile>
        <id>local</id>
        <properties>
            <environment>local</environment>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <port>${tomcat.port}</port>
                        <path>/great</path>
                    </configuration>
                    <executions>
                        <execution>
                            <id>start-tomcat</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <fork>true</fork>
                            </configuration>
                        </execution>
                        <execution>
                            <id>stop-tomcat</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>shutdown</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...> <display-name>My Awesome REST API App</display-name> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 中的

是RootModule.java类:

gumshoe.guicy.hello

public class RootModule extends JerseyServletModule { @Override protected void configureServlets() { bind(GuiceContainer.class); bind(GreeterService.class); serve("/*").with(GuiceContainer.class); } } 中的GreeterService.java类:

gumshoe.guicy.hello.resources

1 个答案:

答案 0 :(得分:0)

好的,我们让它运转起来 代码/文件库存在一系列问题。

1)在web.xml中忽略了一个停止整个引导过程的监听器。

2)存在文件结构问题,因为我的Java软件包位于src而不是Maven所期望的src/main/java

3)存在一些重叠并导致冲突的传递依赖问题。这些是使用pom.xml中的排除标记修复的。

4)我们缺少一些与Netflix OSS相关的配置文件。

它以http://localhost:8889/great/bla/stuff

作出回应

如果你想这样做......不要......除非你真的需要它。

这里有龙......

功能文件集如下所示:

的web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins...>
    <display-name>My Awesome REST API App</display-name>

    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>com.netflix.karyon.server.guice.KaryonGuiceContextListener</listener-class>
    </listener>
</web-app>

新文件:config.properties.../src/main/resources/

archaius.deployment.applicationId=helloworldservice
com.netflix.karyon.eureka.disable=true

helloworldservice.properties

中的新文件.../src/main/resources/
com.netflix.karyon.server.bootstrap.class=gumshoe.guicy.hello.Bootstrap
netflix.platform.admin.resources.port=9092

gumshoe.hello.guicy.RootModule.java

public class RootModule extends JerseyServletModule {

@Override
protected void configureServlets() {

    final HashMap<String, String> initParams = new HashMap<String, String>();

    initParams.put(PackagesResourceConfig.PROPERTY_PACKAGES,
            "gumshoe.guicy.hello");

    bind(GuiceContainer.class);

    bind(GreeterService.class);

    serve("/*").with(GuiceContainer.class);
}
}

gumshoe.guicy.hello.resources.GreeterService.java

@Path("/bla")
public class GreeterService {

    @GET
    @Path("/stuff")
    @Produces("text/plain")
    public String greet() {
        return "Hello World!";
    }
}

新课程:gumshoe.guicy.hello.Bootstrap.java

public class Bootstrap extends ServerBootstrap {

    @Override
    protected void configureBootstrapBinder(BootstrapBinder binder) {


        System.out.println("Bootstrap");

        binder.install(new RootModule());

    }
}

最后但并非最不重要的是,在插件运行时下添加了一堆依赖项和archaius标记的pom.xml:

<plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <configuration>
                        <port>${tomcat.port}</port>
                        <path>/great</path>
                        <systemProperties>
                            <archaius.deployment.applicationId>helloworldservice</archaius.deployment.applicationId>
                        </systemProperties>
                    </configuration>
                    <executions>
                        <execution>
                            <id>start-tomcat</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <fork>true</fork>
                            </configuration>
                        </execution>
                        <execution>
                            <id>stop-tomcat</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>shutdown</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>