0


My problem is that I'm trying to deploy my spring-based webapp on Tomcat. Out of being used to that I embedded jetty for tests, and it worked fine. Whole app is responding correctly when I stop tomcat and just run it. Problem begins when I try to deploy it on Tomcat (it needs to be deployed that way, becouse that is how it will be deployed in production).

My Main.class:

package pl.tracer.config;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.webapp.WebAppContext;

public class Main {

private static Logger logger = Logger.getLogger(Main.class.getName());

public static void main(final String[] aArgs) throws Exception {

    logger.log(Level.INFO, "Starting");
    final Server server = new Server(8080);
    server.setHandler(createWebAppContext());

    server.start();
    server.join();
}

private static WebAppContext createWebAppContext() {
    final WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    context.setBaseResource(new ResourceCollection(
            new String[] { "./src/main/java/META-INF/webapp" }));
    logger.log(Level.INFO, "Created context");
    return context;
}
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>RunTracer</display-name>

<servlet>
    <servlet-name>tracer</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>tracer</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

tracer-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="pl.tracer" />
<mvc:annotation-driven />
</beans>

The only (simple) controller:

package pl.tracer.requestHandling;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.tracer.entities.Track;

@Controller
@RequestMapping(value = "/")
public class TrackController {

@Autowired
private TrackService service;

@ResponseBody
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Map<Long, List<Track>> getAll(
        @RequestParam(required = true) List<Long> ids,
        @RequestParam(required = false) Integer count) {
    Map<Long, List<Track>> map = new HashMap<>();
    for (Long id : ids) {
        List<Track> tracks = service.getAll(id, count);
        map.put(id, tracks);
    }
    return map;
}
}

And 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<groupId>tracer-server</groupId>
<artifactId>tracer-server</artifactId>
<version>1.0</version>
<name>TracerServer</name>

<properties>
    <!-- Spring -->
    <spring-framework.version>4.0.3.RELEASE</spring-framework.version>

    <!-- Baza danych -->
    <hibernate-entitymanager.version>4.2.6.Final</hibernate-entitymanager.version>
    <hibernate-validator.version>4.3.1.Final</hibernate-validator.version>
    <spring-data-jpa.version>1.4.1.RELEASE</spring-data-jpa.version>
    <mysql.version>5.1.6</mysql.version>

    <!-- Jetty -->
    <jetty.version>9.1.0.RC2</jetty.version>

    <!-- Jackson -->
    <jackson.version>2.3.0</jackson.version>
</properties>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Jetty -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>${jetty.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>${jetty.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>${jetty.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlets</artifactId>
        <version>${jetty.version}</version>
    </dependency>

    <!-- Jackson, mapper potrzebny by można było mapować jsona na obiekty -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate-entitymanager.version}</version>
    </dependency>

    <!-- Hibernate validation -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate-validator.version}</version>
    </dependency>

    <!-- Spring and Transactions -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Spring jdbc/orm/data jpa, wszystko potrzebne do poprawnej komunikacji 
        springa z bazą -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring-data-jpa.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory>
                <warName>tracer</warName>
                <archive>
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifest>
                        <addClasspath>false</addClasspath>
                    </manifest>
                    <manifestEntries />
                    <manifestFile />
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}-${project.version}</finalName>
</build>

As You can see, it's as simple as can be. I also attach logs from tomcat:

cze 29, 2014 10:51:12 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
cze 29, 2014 10:51:12 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 593 ms
cze 29, 2014 10:51:12 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
cze 29, 2014 10:51:12 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.26
cze 29, 2014 10:51:12 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/host-    manager.xml
cze 29, 2014 10:51:13 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/manager.xml
cze 29, 2014 10:51:13 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat7/webapps/tracer.war
cze 29, 2014 10:51:13 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/var/lib/tomcat7/webapps/tracer/WEB-INF/lib/javax.servlet-    3.0.0.v201112011016.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending     class: javax/servlet/Servlet.class
cze 29, 2014 10:51:13 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/var/lib/tomcat7/webapps/tracer/WEB-INF/lib/javax.servlet-api-    3.1.0.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class:     javax/servlet/Servlet.class
cze 29, 2014 10:51:13 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/var/lib/tomcat7/webapps/tracer/WEB-INF/lib/jsp-api-2.1-glassfish-    2.1.v20100127.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class:     javax/el/Expression.class
cze 29, 2014 10:51:16 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://www.springframework.org/tags is already defined
cze 29, 2014 10:51:16 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://www.springframework.org/tags/form is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is     already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already     defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /var/lib/tomcat7/webapps/ROOT
cze 29, 2014 10:51:17 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
cze 29, 2014 10:51:17 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5128 ms

Can someone help me, and say why this app doesn't let me perform simple GET on

"http://localhost:8080/tracer?ids=1"

? I always receive 404 error.

Just to assure, that deploying webapps with embedded jetty on tomcat is possible- I have tried this few times and it worked.

Pierwola
  • 135
  • 1
  • 1
  • 14

1 Answers1

1

Embedded jetty has no connection to this. That main class that you have which starts the jetty up is just passive and won't be invoked when deploying this as a webapp in tomcat.

This line caught my eye

    new String[] { "./src/main/java/META-INF/webapp" }));

Looks like your webroot is not the usual place. I guess in that case you should tell tomcat about that using docBase. You can see that in the manual or in places like Change the root context to other folder in tomcat 6

Also there are some jar conflicts (basically servlet-api etc). Did you check if the application was started properly - either in tomcat logs or the manager app? I see you've started that. I guess the url goes something like http://localhost:8080/manager or so

Community
  • 1
  • 1
ac3
  • 196
  • 2
  • You are right. All that was needed to be done was to move my webapp folder from src/main/java/META-INF/ to src/main/. Thanks a lot :) – Pierwola Jun 30 '14 at 14:52