gpt4 book ai didi

java - 使用 Maven 将 Spring 框架应用程序打包到 war

转载 作者:行者123 更新时间:2023-11-28 23:29:58 25 4
gpt4 key购买 nike

我正在关注一个简​​单的官方 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 -> Run on Server 选择Tomcat server,加载内容失败,错误页面看起来像这样:

enter image description here

我不知道如何解决这个问题。我认为我的 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>

最佳答案

您还需要在子项目中指定依赖项。这在此处的 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

关于java - 使用 Maven 将 Spring 框架应用程序打包到 war,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31802807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com