简体   繁体   English

在Tomcat7上部署WebApp

[英]Deploying webapp on Tomcat7


My problem is that I'm trying to deploy my spring-based webapp on Tomcat. 我的问题是我试图在Tomcat上部署基于spring的webapp。 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. 当我停止tomcat并运行它时,整个应用程序都正确响应。 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). 当我尝试将其部署到Tomcat时,问题就开始了(因为需要将其部署在生产环境中,因此需要采用这种方式进行部署)。

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: 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: 示踪剂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: 和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: 我还附上来自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 有人可以帮我吗,并说为什么这个应用程序不能让我执行简单的GET操作

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

? I always receive 404 error. 我总是会收到404错误。

Just to assure, that deploying webapps with embedded jetty on tomcat is possible- I have tried this few times and it worked. 只是为了确保,可以在tomcat上部署带有嵌入式码头的Web应用程序-我已经尝试了几次,并且奏效。

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. 您拥有的启动码头的主类只是被动的,在将其作为WebApp部署在tomcat中时不会被调用。

This line caught my eye 这条线引起了我的注意

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

Looks like your webroot is not the usual place. 看起来您的webroot不在通常的地方。 I guess in that case you should tell tomcat about that using docBase. 我猜在那种情况下,您应该使用docBase告诉tomcat有关。 You can see that in the manual or in places like Change the root context to other folder in tomcat 6 您可以在手册中或类似位置中看到将root上下文更改为tomcat 6中的其他文件夹

Also there are some jar conflicts (basically servlet-api etc). 也有一些jar冲突(基本上是servlet-api等)。 Did you check if the application was started properly - either in tomcat logs or the manager app? 您是否在tomcat日志或管理器应用程序中检查了应用程序是否已正确启动? I see you've started that. 我看到你已经开始了。 I guess the url goes something like http://localhost:8080/manager or so 我想网址会像http://localhost:8080/manager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM