g3blv
g3blv

Reputation: 4377

Running REST service from Intellij with Maven, Jersey and Tomcat 8

I'm trying to run an example for a REST service using Ubuntu 16.04, Oracle Java 8, Intellij with Maven, Jersey and Tomcat 8. I'm following the tutorial that can be found here https://medium.com/@jamsesso/starting-out-with-jersey-apache-tomcat-using-intellij-6338d93ffd40#.heb2u4ros. The example is using Tomcat 7 while I'm using Tomcat 8. When I run the code which sends me to http://localhost:8080/ (but I think it should be http://localhost:8080/hello I've tried it and it gives me a 404 also) I don't get any warnings from Intellij but I get HTTP Status 404 -type Status report message description The requested resource is not available. Apache Tomcat/8.0.32 (Ubuntu) in the web browser. Starting the Tomcat server from command line works fine and gives me the welcome message. I've gone through all the similar questions I can find on stackoverflow but I haven't been able to solve the problem. I would be very happy for any suggestions on what is wrong with my code.

My code is the following.

web.xml
<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>

<servlet>
    <servlet-name>Example API</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.example.jersey</param-value>
    </init-param>

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>Example API</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

_

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.jersey</groupId>
<artifactId>RESTServer</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.18.3</version>
    </dependency>
</dependencies>
</project>

_

package com.example.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloWorld {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getMessage() {
        return "Hello world!";
    }
}

Output from Intellij

/usr/share/tomcat8/bin/catalina.sh run
[2016-07-10 08:17:15,670] Artifact RESTServer:war exploded: Server is not connected. Deploy is not available.
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.32 (Ubuntu)
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jul 6 2016 11:49:29 UTC
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.32.0
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Linux
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            4.4.0-28-generic
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /usr/lib/jvm/java-8-oracle/jre
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_91-b14
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /home/mikael/.IntelliJIdea2016.1/system/tomcat/Unnamed_RESTServer_2
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /usr/share/tomcat8
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.config.file=/home/mikael/.IntelliJIdea2016.1/system/tomcat/Unnamed_RESTServer_2/conf/logging.properties
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote=
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.port=1099
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.ssl=false
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.rmi.server.hostname=127.0.0.1
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/usr/share/tomcat8/endorsed
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/home/mikael/.IntelliJIdea2016.1/system/tomcat/Unnamed_RESTServer_2
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/usr/share/tomcat8
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.io.tmpdir=/home/mikael/.IntelliJIdea2016.1/system/tomcat/Unnamed_RESTServer_2/temp
Jul 10, 2016 8:17:17 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jul 10, 2016 8:17:17 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1586 ms
Jul 10, 2016 8:17:18 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 10, 2016 8:17:18 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.32 (Ubuntu)
Jul 10, 2016 8:17:18 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jul 10, 2016 8:17:18 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 188 ms
Connected to server
[2016-07-10 08:17:18,307] Artifact RESTServer:war exploded: Artifact is being deployed, please wait...
Jul 10, 2016 8:17:19 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2016-07-10 08:17:19,467] Artifact RESTServer:war exploded: Artifact is deployed successfully
[2016-07-10 08:17:19,468] Artifact RESTServer:war exploded: Deploy took 1,161 milliseconds

Upvotes: 2

Views: 5120

Answers (1)

g3blv
g3blv

Reputation: 4377

I managed to get this working. The main issue was the difference in Servlet version and Jersey version that the example uses and what I was able to use. The example is using Servlet 3.0 while I'm using 3.1. I might be wrong here but I think that Servlet 3.1 forces me to use Jersey 2. Both these differences means that the web.xml and the pom.xml files needs to reflect these changes. I've posted my updated web.xml and pom.xml below.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>Example API</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.example.jersey</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Example API</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

-

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.example.jersey</groupId>
    <artifactId>stack</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.13</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.13</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>stack</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <name>Stack</name>
</project>

Upvotes: 2

Related Questions