简体   繁体   English

Spring RESTful服务作为WAR而不是Tomcat中的JAR

[英]Spring RESTful Service as a WAR instead of JAR in Tomcat

I am in the process of creating a REST web service in Java Spring. 我正在使用Java Spring创建REST Web服务。 I've successfully loaded STS and the example detailed at : 我已成功加载STS,示例详述如下:

"This guide walks you through the process of creating a "hello world" RESTful web service with Spring." “本指南将引导您完成使用Spring创建”hello world“RESTful Web服务的过程。” http://spring.io/guides/gs/rest-service/ 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. 然而,该教程只是到目前为止..我想创建一个WAR文件,而不是一个包含servlet的自运行jar,并部署该WAR文件。 I then found this tutorial, and attempted to just modify the first tutorials build.gradle file. 然后我找到了本教程,并试图修改第一个教程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 网址404'd

http://localhost:8080/gs-rest-service-0.1.0/dbgreeting?name=MyName

Do I need to modify the mapping? 我需要修改映射吗?

DataBaseController.java DataBaseController.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. 我发现OSX上的XAMPP不包含webapp /文件夹,这迫使我改为加载Bitnami的Tomcat堆栈。 Do people generally switch between XAMPP and other stacks based on this? 人们通常会在此基础上在XAMPP和其他堆栈之间切换吗? or did I miss something to get webapp folder created in XAMPP? 或者我错过了在XAMPP中创建webapp文件夹的内容?

A WAR is just a JAR with special properites. WAR只是一个特殊的JAR。 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. 它需要一个WEB-INF,在这个WEB-INF下你需要一个web.xml来描述你的部署,任何app server dependentXML配置文件,通常还有lib,类和其他的几率。

The easiest way would be to use Maven to create your WAR. 最简单的方法是使用Maven来创建WAR。 I think you should be able to simply change the project type in the pom.xml from JAR to WAR. 我认为你应该能够简单地将pom.xml中的项目类型从JAR更改为WAR。 The tutorial you followed seems to use Gradle, which in turn uses Maven I believe, so you should have one there somewhere. 您遵循的教程似乎使用Gradle,而Gradle又使用Maven,我相信你应该在那里有一个。 Other than that, google for tutorials on how to construct a WAR. 除此之外,谷歌有关如何构建WAR的教程。 I don't believe that Tomcat requires any special deployment descriptors, so you should only need the web .xml. 我不相信Tomcat需要任何特殊的部署描述符,因此您只需要web .xml。

( 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 /你好/ 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使用它来替换它)。

I would expect to see some INFO output when a spring boot application starts so some ideas are: 我希望在Spring启动应用程序启动时会看到一些INFO输出,所以有些想法是:

Try a regular tomcat instance 尝试一个常规的tomcat实例

  • Download and extract the zip distribution . 下载并解压缩zip分发
  • Start tomcat with bin/startup.sh 使用bin/startup.sh启动tomcat
  • Copy your war to the webapps directory 将您的战争复制到webapps目录
  • Check the logs... hope to see some evidence of spring starting up 检查日志...希望看到弹簧启动的一些证据

Manually inspect the war file 手动检查war文件

  • Unzip your war file 解压缩你的war文件
  • Expect to see WEB-INF/web.xml 期待看到WEB-INF / web.xml

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

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