Java Spring RESTful 服务作为 WAR 而不是 Tomcat 中的 JAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19820029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring RESTful Service as a WAR instead of JAR in Tomcat
提问by Erik
I am in the process of creating a REST web service in Java Spring. I've successfully loaded STS and the example detailed at :
我正在 Java Spring 中创建 REST Web 服务。我已经成功加载了 STS 和详细的示例:
"This guide walks you through the process of creating a "hello world" RESTful web service with Spring." http://spring.io/guides/gs/rest-service/
“本指南将引导您完成使用 Spring 创建“hello world”RESTful Web 服务的过程。” http://spring.io/guides/gs/rest-service/
However that tutorial only goes so far.. I want to create a WAR file instead of a self running jar containing a servlet, and deploy that WAR file. I then found this tutorial, and attempted to just modify the first tutorials build.gradle file.
但是,该教程仅到此为止。我想创建一个 WAR 文件,而不是一个包含 servlet 的自运行 jar,并部署该 WAR 文件。然后我找到了这个教程,并试图修改第一个教程 build.gradle 文件。
"Converting a Spring Boot JAR Application to a WAR" http://spring.io/guides/gs/convert-jar-to-war/
“将 Spring Boot JAR 应用程序转换为 WAR” http://spring.io/guides/gs/convert-jar-to-war/
It seemed to build just fine into a .war file.. the service is running in my TOMCAT instance's manager.. but I get 404's once I attempt to use the service.
它似乎很好地构建到 .war 文件中.. 该服务正在我的 TOMCAT 实例的管理器中运行.. 但是一旦我尝试使用该服务,我就会得到 404。
URL 404'd
URL 404'd
http://localhost:8080/gs-rest-service-0.1.0/dbgreeting?name=MyName
Do I need to modify the mapping?
我需要修改映射吗?
DataBaseController.java
数据库控制器.java
@RequestMapping("/dbgreeting")
public @ResponseBody DataBaseGreeter dbgreeting(
@RequestParam(value="name", required=false, defaultValue="World") String name) {
return new DataBaseGreeter(counter.incrementAndGet(),String.format(template, name));
}
Now I have the .war file created according to a blending of things.. and worried I perhaps missed something.
现在我根据混合的东西创建了 .war 文件......并且担心我可能错过了一些东西。
I've since discovered XAMPP on OSX doesn't contain a webapp/ folder, which has forced me to load Bitnami's Tomcat stack instead. Do people generally switch between XAMPP and other stacks based on this? or did I miss something to get webapp folder created in XAMPP?
我发现 OSX 上的 XAMPP 不包含 webapp/ 文件夹,这迫使我改为加载 Bitnami 的 Tomcat 堆栈。人们通常会基于此在 XAMPP 和其他堆栈之间切换吗?还是我错过了在 XAMPP 中创建 webapp 文件夹的内容?
采纳答案by CodeChimp
A WAR is just a JAR with special properites. It needs to have a WEB-INF, under which you need a web.xml to describe your deployment, any app server dependentXML configuration files, and usually a lib, classes, and other odds and ends.
WAR 只是一个具有特殊属性的 JAR。它需要有一个 WEB-INF,在它下面你需要一个 web.xml 来描述你的部署、任何应用服务器依赖的 XML 配置文件,通常还有一个 lib、类和其他零碎的东西。
The easiest way would be to use Maven to create your WAR. I think you should be able to simply change the project type in the pom.xml from JAR to WAR. The tutorial you followed seems to use Gradle, which in turn uses Maven I believe, so you should have one there somewhere. Other than that, google for tutorials on how to construct a WAR. I don't believe that Tomcat requires any special deployment descriptors, so you should only need the web .xml.
最简单的方法是使用 Maven 创建你的 WAR。我认为您应该能够简单地将 pom.xml 中的项目类型从 JAR 更改为 WAR。您遵循的教程似乎使用 Gradle,我相信它反过来使用 Maven,所以您应该在某处安装一个。除此之外,谷歌有关如何构建 WAR 的教程。我不相信 Tomcat 需要任何特殊的部署描述符,所以你应该只需要 web .xml。
回答by samlewis
I would expect to see some INFO output when a spring boot application starts so some ideas are:
我希望在 Spring Boot 应用程序启动时看到一些 INFO 输出,所以一些想法是:
Try a regular tomcat instance
尝试一个普通的 tomcat 实例
- Download and extract the zip distribution.
- Start tomcat with
bin/startup.sh
- Copy your war to the
webapps
directory - Check the logs... hope to see some evidence of spring starting up
- 下载并解压缩 zip 分发包。
- 启动tomcat
bin/startup.sh
- 将您的战争复制到
webapps
目录 - 检查日志...希望看到一些春天开始的证据
Manually inspect the war file
手动检查war文件
- Unzip your war file
- Expect to see WEB-INF/web.xml
- 解压你的战争文件
- 期待看到 WEB-INF/web.xml
回答by Duncan Jones
(Answer from OP moved from question to here)
(来自 OP 的回答从问题移至此处)
Boy I feel really dumb.. Found there was more to the tutorial after changing the gradle instructions.. including the very needed Auto Configuration that supercedes/replaces the need for a web.xml
男孩,我觉得真的很愚蠢.. 在更改 gradle 说明后发现教程还有更多内容.. 包括非常需要的自动配置,它取代/取代了对 web.xml 的需求
Solution
解决方案
Initialize the servlet
初始化 servlet
Previously, the application contained a public static void main()
method which the spring-boot-gradle-plugin was configured to run when using the java -jar
command.
以前,应用程序包含一个public static void main()
方法,spring-boot-gradle-plugin 被配置为在使用该java -jar
命令时运行。
By converting this into a WAR file with no XML files, you need a different signal to the servlet container on how to launch the application.
通过将其转换为没有 XML 文件的 WAR 文件,您需要向 servlet 容器发出不同的信号,以说明如何启动应用程序。
src/main/java/hello/HelloWebXml.java
src/main/java/hello/HelloWebXml.java
package hello;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.SpringBootServletInitializer;
public class HelloWebXml extends SpringBootServletInitializer {
@Override
protected void configure(SpringApplicationBuilder application) {
application.sources(Application.class);
}
}
Will give credit to the first answer, but you both were correct that the web.xml (or what Spring-Boot uses to replace it) was needed.
将归功于第一个答案,但你们俩都认为需要 web.xml (或 Spring-Boot 用来替换它的内容)是正确的。