0

I have created a jersey web service and deployed it on a tomcat application server. I have the war file deployed in tomcat, but still when I try to access the application a got an error that the requested data could not be found(The standard tomcat 404 error). This is my web.xml file.

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> Jersey index.html

<servlet>
    <servlet-name>Jersey REST Service</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>sample.jsonStrip.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>RestServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

This is my pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.rares.urs Jersey 0.0.1-SNAPSHOT Ingredients

<repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
</repositories>
<packaging>war</packaging>

<build>
    <finalName>JsonStrip</finalName>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <configuration>
                <url>http://127.0.0.1:8888/manager</url>
                <server>mytomcat</server>
                <path>/Jersey</path>
            </configuration>
        </plugin>


        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat8-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://127.0.0.1:8888/manager</url>
                <server>TomcatServer</server>
                <path>/Switch</path>
            </configuration>
        </plugin>

    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.9</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.22.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.19</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20140107</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.19</version>
    </dependency>
</dependencies>

Both of the xml files are in located in Jersey root folder. I have seen some cases when wab.xml is situated in WebContent/WEB-INF/web.xml? Where should I place it?

Thanks!

Rareș Urs
  • 71
  • 12
  • That could have many reasons. What does the log say when starting up? Which url did you hit? Several problems in pom.xml. Web.xml must be placed in WEB-INF/web.xml. – Stefan Jun 27 '16 at 11:03
  • I have a web.xml there but it was auto-generated, should I replace the content with the one I have created? I go to http://127.0.0.1:8889/JsonStrip/rest/jersey, but I have tried all the possible combinations. What errors do I have in pom? – Rareș Urs Jun 27 '16 at 11:11
  • The `` in the web.xml need to match in both locations. – Paul Samsotha Jun 27 '16 at 11:35

1 Answers1

1

The <servlet-name> in the web.xml need to match in both locations

In the <servlet-mapping>, the <servlet-name> tells the container what servlet this mapping should apply to, by looking for a <servlet> with the same <servlet-name>. Right now your Jersey servlet has no URL mapping, so it won't handle any requests.

I have seen some cases when wab.xml is situated in WebContent/WEB-INF/web.xml? Where should I place it?

There. I haven't used eclipse in a while, but I'm pretty sure the WebContent folder isn't a "real" folder. It is only an IDE display folder. It should map in the real directory structure to src/main/webapp in a Maven application. That's where the web.xml should be src/main/webapp/WEB-INF

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I have replaced the name in both tags to RestServlet, now the app should launch with http://127.0.0.1:8889/Jersey/RestServlet/(and_the_path_to_a_class) ? – Rareș Urs Jun 27 '16 at 12:06
  • Either that or replace `Jersey` with `JsonStrip`. Depends on your Eclipse context configuration – Paul Samsotha Jun 27 '16 at 12:11
  • Thank you very much! Maybe my folder structure is wrong, I keep getting the same error – Rareș Urs Jun 27 '16 at 12:33
  • Try deleting `WebContent`. Like I said, I don't even think that is a real folder. Look in your _real_ directory. It should be `src/main/webapp`. There should be no `WebContent`. Like I said, that is only an IDE Display directory – Paul Samsotha Jun 27 '16 at 12:35
  • You might want to just check [this out](http://stackoverflow.com/a/30020830/2587435) – Paul Samsotha Jun 27 '16 at 12:37
  • In src I have all the java files, no web app. One folder up, I have WebContent folder which has WEB-INF/web.xml. – Rareș Urs Jun 27 '16 at 12:41
  • OK well you don't even have a correct maven project then. See my link in the previous comment – Paul Samsotha Jun 27 '16 at 12:42