3

I have a Jersey 2 Maven project.

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>com.api.rest.ApplicationResource</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>        
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.api.rest.ApplicationResource</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>

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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.api</groupId>
    <artifactId>GLI_API</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>GLI_API</name>

    <build>
        <finalName>GLI_API</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.21</version>
        </dependency>


        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP-java6</artifactId>
            <version>2.2.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.mindrot</groupId>
            <artifactId>jbcrypt</artifactId>
            <version>0.3m</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>com.cloudinary</groupId>
            <artifactId>cloudinary-http44</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.19</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

My project works fine when working locally. But it doesn't work anymore when i deploy it on my remote server.

So what happens is basically, that the WAR gets deployed correctly but there is really no error or warning in catalina.out. I get a 404 on every of my defined endpoints.

catalina.out:

Sep 14, 2015 2:03:00 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat7/webapps/GLI_API.war
Sep 14, 2015 2:04:17 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat7/webapps/ICDS_API.war
Sep 14, 2015 2:04:23 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /var/lib/tomcat7/webapps/ROOT
Sep 14, 2015 2:04:23 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 14, 2015 2:04:23 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 84867 ms

ApplicationResource:

package com.api.rest;

import java.util.logging.Logger;


import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import com.api.provider.ResponseCorsFilter;
import org.glassfish.jersey.media.multipart.MultiPartFeature;


public class ApplicationResource extends ResourceConfig {

    private static final Logger LOGGER = null;   


    public ApplicationResource() {
        // Register resources and providers using package-scanning.
        packages("com.api");
        System.out.println("started");

        register(MultiPartFeature.class);

        // Register my custom provider - not needed if it's in my.package.
        register(ResponseCorsFilter.class);
        // Register an instance of LoggingFilter.
        register(new LoggingFilter(LOGGER, true));

        // Enable Tracing support.
        property(ServerProperties.TRACING, "ALL");
    }
}

What could be the problem? I developed on tomcat8 (but don't remember using something specifically) and deployed on tomcat7. Could this be a problem?


This endpoint should be reachable: http://54.228.220.152:8080/GLI_API/api/item

The other API works fine - so the server in general is working.

Also - if you don't have the final answer i would really appreciate it if you can point me to files/directories to look at since i don't get any error.

Stefan
  • 12,108
  • 5
  • 47
  • 66
Fabian Lurz
  • 2,029
  • 6
  • 26
  • 52

1 Answers1

3

You may try to enable annotation scanning by setting this init parameter to your servlet:

<servlet>
 ...
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

 <init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.yourproject.packageWhereYourResourcesAre</param-value>
 </init-param>
...
</servlet>

Update

It seems you have compiled against Java 8, while Tomcat runs with Java 7? Try this:

<configuration>
    <source>1.7</source>
    <target>1.7</target>
</configuration>

You may also want to update your web.xml to 3.0:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
Stefan
  • 12,108
  • 5
  • 47
  • 66
  • Great thanks - this helped. I get an unsupported major minor version now where i can work with. I thought i have this in my controller: packages("com.api"); What is the difference? I'm sometimes very confused between web.xml and my ApplicationController – Fabian Lurz Sep 14 '15 at 14:54
  • Result: I compiled in JDK 1.8 but don't have it on the server - ouch :( – Fabian Lurz Sep 14 '15 at 15:01