Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...