Spring RESTful Service as a WAR instead of JAR in Tomcat

前端 未结 3 2153
感动是毒
感动是毒 2020-12-15 19:21

I am in the process of creating a REST web service in Java Spring. I\'ve successfully loaded STS and the example detailed at :

\"This guide walks you through the pr

相关标签:
3条回答
  • 2020-12-15 19:38

    (Answer from OP moved from question to here)

    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

    Solution

    Initialize the 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.

    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.

    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.

    0 讨论(0)
  • 2020-12-15 19:38

    I would expect to see some INFO output when a spring boot application starts so some ideas are:

    Try a regular tomcat instance

    • 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

    Manually inspect the war file

    • Unzip your war file
    • Expect to see WEB-INF/web.xml
    0 讨论(0)
  • 2020-12-15 19:49

    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.

    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.

    0 讨论(0)
提交回复
热议问题