簡體   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