java Tomcat RESTful Web 服务部署
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31657641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tomcat RESTful Web service deployment
提问by Tony DING
I'm writing a simple RESTful web service using Java, tomcat7, jersey and the IDE eclipse.
我正在使用 Java、tomcat7、jersey 和 IDE eclipse 编写一个简单的 RESTful Web 服务。
When I launched the web service using eclipse (Servers), it works well. I tested the GET and POST method. But when I export the application in WAR file and deployed with tomcat manage UI. It returns status 404 not found.
当我使用 eclipse(服务器)启动 Web 服务时,它运行良好。我测试了 GET 和 POST 方法。但是当我在 WAR 文件中导出应用程序并使用 tomcat 管理 UI 部署时。它返回状态 404 not found。
Here is the example:
这是示例:
@Path("/webservice")
public class WebService {
@POST
@Path("/post")
@Produces(MediaType.APPLICATION_JSON)
public Response helloWorld(String inputJson) {
return Response.ok().entity("Hello World").build();
}
@GET
@Path("/{param}")
public Response getMessage(@PathParam("param") String message) {
String output = "Jersey say Hello World!!! : " + message;
return Response.status(200).entity(output).build();
}
}
Here is the web.xml:
这是 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WebService</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>package.webservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Can anybody explain what's the difference between the launch the service in eclipse and deploy in localhost (OR remote host)? And how can I debug or get some traces about this?
任何人都可以解释在 eclipse 中启动服务和在本地主机(或远程主机)中部署之间的区别是什么?我怎样才能调试或得到一些关于这个的痕迹?
回答by M. M. Saleh Salman
there are 2 suggestion for you to get rid from this problem 1) in your resource file make a default method so that if no url match then it will invoke otherwise it may give 404
有 2 个建议可以让您摆脱这个问题 1) 在您的资源文件中创建一个默认方法,以便如果没有 url 匹配则它将调用,否则它可能会给出 404
@GET
@Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN })
public String default() {
return "Hello Rest Api";
}
you can see -> Rest api resource example
你可以看到 -> Rest api 资源示例
2) set a default rest api path in your web.xml lke below
2) 在你的 web.xml lke 下面设置一个默认的 rest api 路径
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
you can see -> Rest api default path set
你可以看到 -> Rest api 默认路径集
so that when you call your api like -> http://something.com/project/restthen your default method of the resource file will fire. so no 404 happen.
这样当你像 -> http://something.com/project/rest这样调用你的 api 时,你 的资源文件的默认方法就会被触发。所以没有 404 发生。
回答by Tony DING
I finally get it working. I set the context-root in the properties of the eclipse project. The URL accessible will be something like: localhost:8080/context-root/rest/... But when I deploy this with WAR file in Tomcat, this configuration is not taken into account. The correct URL is still: localhost:8080/project/rest/...
我终于让它工作了。我在 eclipse 项目的属性中设置了上下文根。可访问的 URL 将类似于: localhost:8080/context-root/rest/... 但是当我在 Tomcat 中使用 WAR 文件部署它时,不会考虑此配置。正确的 URL 仍然是:localhost:8080/project/rest/...
I have to find how to set the context-root in web.xml or somewhere else.
我必须找到如何在 web.xml 或其他地方设置上下文根。
回答by Rahul Varadkar
The web.xml settings for running REST Api's using Jersey is best explained in following URL.
在以下 URL 中最好地解释了使用 Jersey 运行 REST Api 的 web.xml 设置。
http://www.vogella.com/tutorials/REST/article.html#jerseyprojectsetup_gradle
http://www.vogella.com/tutorials/REST/article.html#jerseyprojectsetup_gradle
I am developing REST Api's and following is the web.xml setting.
我正在开发 REST Api,以下是 web.xml 设置。
<servlet>
<servlet-name>BOARDWALK REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>io.swagger.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>BOARDWALK REST Service</servlet-name>
<url-pattern>/bae/*</url-pattern>
</servlet-mapping>
All REST API Service classes are stored under classes/io/swagger/api folder. When I call a REST api, I use following URL and it works.
所有 REST API 服务类都存储在 classes/io/swagger/api 文件夹下。当我调用 REST api 时,我使用以下 URL 并且它可以工作。
http://localhost:8080/bae4_3_release/bae/bcpInstance
http://localhost:8080/bae4_3_release/bae/bcpInstance
where http://localhost:8080/bae4_3_releaseis the context. /bae/bcpInstanceis pointing to a class in classes/io/swagger/api/BcpInstanceApi.classthat has PATH as @bcpInstance defined in it.
其中 http://localhost:8080/bae4_3_release是上下文。 /bae/bcpInstance指向classes/io/swagger/api/BcpInstanceApi.class中的一个类,其中定义了 PATH 作为 @bcpInstance。
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>io.swagger.api</param-value>
</init-param>
Denotes location of REST APIs deployment.
表示 REST API 部署的位置。