本文介绍了在Tomcat中将Spring RESTful Service作为WAR而不是JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java Spring中创建REST Web服务。我已经成功加载了STS,并且详细介绍了这个示例:

本指南将引导您完成使用Spring创建hello worldRESTful Web服务的过程。





URL 404'd


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

我需要修改映射吗?



DataBaseController.java

  @RequestMapping( / dbgreeting)
public @ResponseBody DataBaseGreeter dbgreeting(
@RequestParam(value =name,required = false,defaultValue =World)字符串名称){
return new DataBaseGreeter counter.incrementAndGet(),String.format(template,name));
}

现在我有根据混合创建的.war文件。并担心我可能错过了什么。



自从在OSX上发现XAMPP不包含webapp /文件夹,它迫使我加载Bitnami的Tomcat堆栈。人们通常会在XAMPP和其他堆栈之间切换吗?或者我错过了什么来获取在XAMPP中创建的webapp文件夹?

解决方案

WAR只是一个具有特殊特性的JAR。它需要一个WEB-INF,在这个WEB-INF下你需要一个web.xml来描述你的部署,任何应用服务器依赖的XML配置文件,通常还有一个lib,classes和其他的可能性和结束。



最简单的方法是使用Maven创建WAR。我想你应该能够简单地将pom.xml中的项目类型从JAR更改为WAR。你所遵循的教程似乎使用Gradle,它反过来使用Maven我相信,所以你应该有一个地方。除此之外,谷歌的教程如何构建一个战争。我不相信Tomcat需要任何特殊的部署描述符,所以你应该只需要web
.xml。


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.

这篇关于在Tomcat中将Spring RESTful Service作为WAR而不是JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:53