0

Okay, so I cannot get the HelloWorld from JerseyGuice working for the life of me. I am sure I am missing something simple and stupid, so I am just going to throw all of this out there:

It is supposed to be run via Maven command line like: mvn tomcat7:run

It should respond on: http://localhost/great/stuff

pom.xml: (run profile is at very bottom)

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

in gumshoe.guicy.hello is the RootModule.java class:

public class RootModule extends JerseyServletModule {

@Override
protected void configureServlets() {

    bind(GuiceContainer.class);

    bind(GreeterService.class);

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

and in gumshoe.guicy.hello.resources is the GreeterService.java class:

public class GreeterService {

    @GET
    @Path("/stuff")
    @Produces("text/plain")
    public String greet(final HttpServletRequest request) {
        return "Hello World!";
    }
}
adv
  • 357
  • 5
  • 18
  • 1) `PackagesResourceConfig("jersey.resources.package");`. You're telling Jersey to scan the `jersey.resources.package` for your resource classes. They're not in there. 2) `GreetingService` needs a `@Path` annotation to considered a resource class. 3) `HttpServletRequest` can't be injected without a `@Context` annotation. Other than say, I can't say much for your Guice configuration; I don't ever use Guice. – Paul Samsotha Apr 10 '18 at 23:03
  • 1) I took from another random answer and threw it in there just to see. – adv Apr 11 '18 at 12:37
  • There was a Maven build problem also that is related to this: https://stackoverflow.com/questions/42525139/maven-build-compilation-error-failed-to-execute-goal-org-apache-maven-plugins And using JDK9. The Tomcat runs, but there is nothing in the ...\webapps or ..\work\Tomcat\localhost\great ... I think it may be a Maven problem more than a Jersey or Guice problem. – adv Apr 11 '18 at 14:01

1 Answers1

0

Okay, we got it working.
There were a series of issues with the code/file base.

1) There was a listener neglected in the web.xml that was stalling the entire bootstrap process.

2) There was a file structure problem in that my Java packages were in src and not in src/main/java as Maven expected.

3) There were some transitive dependency issues that were overlapping and causing conflicts. These were fixed using exclusion tags in the pom.xml.

4) We were missing some config files related to the Netflix OSS.

It responds on http://localhost:8889/great/bla/stuff

If you are trying to do this... don't... unless you really need it.

There be dragons here...


The functional file set looks like:

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>

New file: config.properties: in .../src/main/resources/

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

New file: helloworldservice.properties in .../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!";
    }
}

New class: gumshoe.guicy.hello.Bootstrap.java:

public class Bootstrap extends ServerBootstrap {

    @Override
    protected void configureBootstrapBinder(BootstrapBinder binder) {


        System.out.println("Bootstrap");

        binder.install(new RootModule());

    }
}

and last but not least, the pom.xml to which was added a bunch of dependencies and an archaius tag, under the plugin runtime:

<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>
adv
  • 357
  • 5
  • 18