我的问题是我试图在Tomcat上部署基于Spring的webapp。出于习惯,我将嵌入式码头用于测试,并且工作正常。当我停止tomcat并运行它时,整个应用程序正确响应。当我尝试在Tomcat上部署它时问题就开始了(它需要以这种方式部署,因为它将如何在生产中部署)。
我的主要课程:
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>
示踪剂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>
唯一(简单)控制器:
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;
}
}
和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>
正如您所看到的,它尽可能简单。 我还附加了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
有人可以帮助我,并说明为什么这个应用程序不允许我执行简单的GET
"http://localhost:8080/tracer?ids=1"
?我总是收到404错误。
只是为了确保在tomcat上部署带有嵌入式jetty的webapp是可能的 - 我已经尝试了几次并且它有效。
答案 0 :(得分:1)
嵌入式码头与此无关。启动jetty的那个主类只是被动的,在tomcat中将它作为webapp部署时不会被调用。
这条线引起了我的注意
new String[] { "./src/main/java/META-INF/webapp" }));
看起来您的webroot不是通常的地方。我想在那种情况下你应该告诉tomcat使用docBase。您可以在手册或Change the root context to other folder in tomcat 6
等地方查看还有一些jar冲突(基本上是servlet-api等)。您是否在tomcat日志或经理应用程序中检查应用程序是否已正确启动?我看到你已经开始了。我猜网址类似于http://localhost:8080/manager
左右