简体   繁体   中英

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. I've successfully loaded STS and the example detailed at :

"This guide walks you through the process of creating a "hello world" RESTful web service with Spring." 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.

"Converting a Spring Boot JAR Application to a 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.

URL 404'd

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

Do I need to modify the mapping?

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.

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?

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.

( 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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