繁体   English   中英

通过Maven Tomcat的Jersey Guice Servlet-404

[英]Jersey Guice Servlet via Maven Tomcat - 404

好的,所以我无法从JerseyGuice获得HelloWorld,这对我一生都是有效的。 我确信我缺少一些简单而愚蠢的东西,所以我将把所有这些都扔掉:

它应该通过Maven命令行运行,例如: mvn tomcat7:run

它应该响应: http:// localhost / great / stuff

pom.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>

web.xml:

<?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>

gumshoe.guicy.hello是RootModule.java类:

public class RootModule extends JerseyServletModule {

@Override
protected void configureServlets() {

    bind(GuiceContainer.class);

    bind(GreeterService.class);

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

gumshoe.guicy.hello.resources是GreeterService.java类:

public class GreeterService {

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

好吧,我们让它工作了。
代码/文件库存在一系列问题。

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

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

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM