【问题标题】:Package Spring framework application to war using Maven使用 Maven 将 Spring 框架应用程序打包到 war
【发布时间】:2015-08-04 07:08:14
【问题描述】:

我正在关注一个简​​单的官方 Spring 教程指南Enabling Cross Origin Requests for a RESTful Web Service,您可以从其github repo 下载文件。

我正在尝试使用 Maven 将此应用程序打包为 war 文件并将其部署在 tomcat 上,因此我在 Packaging executable jar and war files 上关注另一个官方 Spring 文档并更改了 pom.xml 使其具有:

<packaging>war</packaging>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

我也把java版本改成7:

<properties>
    <java.version>1.7</java.version>
</properties>

我的IDE是Eclipse Luna 4.4.2,所以我保存了新的pom.xml文件,右键项目,选择Maven -> updated the project,然后我右键项目,选择run as -> @ 987654335@选择Tomcat服务器,加载内容失败,错误页面如下:

我不知道如何解决这个问题。我认为我的 tomcat 还可以,因为我可以在上面运行其他 Web 应用程序。我的操作系统是 Windows 7 64 位,Java 是 JDK 7,maven 是 3.3.3

Tomcat 错误跟踪:

...
INFO: Starting Servlet Engine: Apache Tomcat/8.0.23
Aug 04, 2015 12:10:12 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.
Aug 04, 2015 12:10:12 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error during ServletContainerInitializer processing
javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:160)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(FutureTask.java:274)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1157)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:627)
    at java.lang.Thread.run(Thread.java:809)
Caused by: java.lang.NoClassDefFoundError: org.glassfish.jersey.server.ResourceConfig
    at java.lang.J9VMInternals.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1920)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:157)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.server.ResourceConfig
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
    ... 11 more

Aug 04, 2015 12:10:12 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/gs-rest-service-cors] startup failed due to previous errors
Aug 04, 2015 12:10:12 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Aug 04, 2015 12:10:12 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Aug 04, 2015 12:10:12 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2439 ms

填写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>org.springframework</groupId>
    <artifactId>gs-rest-service-cors</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.7</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

【问题讨论】:

  • 此项目未部署或如您提到的项目名称地址栏。您在web.xml 文件中添加了任何登录页面文件?尝试从地址栏调用一些jsp页面(页面的完整路径)
  • 如果您使用&lt;scope&gt;provided&lt;/scope&gt;,这意味着它应该在您将其部署到的环境中......换句话说,它不会被打包到适当的war文件中。如果您想打包,只需删除行&lt;scope&gt;provided&lt;/scope&gt;
  • @khmarbaise,我删除了&lt;scope&gt;provided&lt;/scope&gt;这一行并重新部署项目,出现同样的错误。
  • 你能发布完整的 POM 吗?泽西依赖项似乎有问题。您是否按照here 的描述提供SpringBootServletInitializer
  • @SteffenKreutz,添加了完整的 POM。

标签: java eclipse spring maven tomcat


【解决方案1】:

您还需要在子项目中指定依赖项。这在此处的 maven 指南中有所描述http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management

因此,在您的情况下,您需要在 pom.xml 中添加此依赖项

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
</dependency>

解决您的下一个问题。您的war文件中的Application类似乎还没有为传统部署做好准备。因此,您需要将其更改为以下内容:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

如需进一步参考,您可以查看link

【讨论】:

  • 我试过你的解决方案,jersey依赖的错误消失了,但是页面仍然没有找到,它应该按预期返回json文件。
  • 您好,日志中是否有任何错误消息或堆栈跟踪?
  • tomcat 日志中没有错误但没有正确部署,浏览器仍然显示与上图相同的 http 状态 404 错误。您可以在您的机器上正确获取 json 响应吗?
  • 是的,我能够得到 json 响应。你试过这个网址吗/gs-rest-service-cors-0.1.0/greeting?因为您没有为路径 / 映射任何控制器。
  • 我尝试了gs-rest-service-cors-0.1.0/greeting,但仍然收到 404 错误。
猜你喜欢
  • 1970-01-01
  • 2019-08-10
  • 2014-10-21
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多