Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 422
Erik Avatar asked Nov 06 '13 18:11

Erik


1 Answers

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.

like image 109
CodeChimp Avatar answered Sep 23 '22 19:09

CodeChimp