简体   繁体   English

Tomcat RESTful Web 服务部署

[英]Tomcat RESTful Web service deployment

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.当我使用 eclipse(服务器)启动 web 服务时,它运行良好。 I tested the GET and POST method.我测试了 GET 和 POST 方法。 But when I export the application in WAR file and deployed with tomcat manage UI.但是当我在 WAR 文件中导出应用程序并使用 tomcat 管理 UI 进行部署时。 It returns status 404 not found.它返回状态 404 未找到。

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)?谁能解释在 eclipse 中启动服务和在本地主机(或远程主机)中部署有什么区别? And how can I debug or get some traces about this?我怎样才能调试或得到一些关于这个的痕迹?

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/rest then your default method of the resource file will fire. 所以,当你打电话给你的api - > http://something.com/project/rest然后你的资源文件的默认方法将被触发。 so no 404 happen. 所以没有404发生。

I finally get it working. 我终于开始工作了。 I set the context-root in the properties of the eclipse project. 我在eclipse项目的属性中设置了context-root。 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. 可访问的URL将类似于:localhost:8080 / context-root / rest / ...但是当我在Tomcat中使用WAR文件部署它时,不会考虑此配置。 The correct URL is still: localhost:8080/project/rest/... 正确的URL仍然是:localhost:8080 / project / rest / ...

I have to find how to set the context-root in web.xml or somewhere else. 我必须找到如何在web.xml或其他地方设置context-root。

The web.xml settings for running REST Api's using Jersey is best explained in following URL. 使用Jersey运行REST Api的web.xml设置最好在以下URL中进行说明。

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. 所有REST API服务类都存储在classes / io / swagger / api文件夹下。 When I call a REST api, I use following URL and it works. 当我调用REST api时,我使用以下URL并且它可以工作。

http://localhost:8080/bae4_3_release/bae/bcpInstance HTTP://本地主机:8080 / bae4_3_release / BAE / bcpInstance

where http://localhost:8080/bae4_3_release is the context. 其中http:// localhost:8080 / bae4_3_release是上下文。 /bae/bcpInstance is pointing to a class in classes/io/swagger/api/BcpInstanceApi.class that has PATH as @bcpInstance defined in it. / 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部署的位置。

I've fixed it by putting the right JDK on env variables.我通过将正确的 JDK 放在 env 变量上来修复它。 Setting up JAVA_HOME to point to Java 11 instead of Java 8.将 JAVA_HOME 设置为指向 Java 11 而不是 Java 8。

I had the same issue with Tomcat 9 and IntelliJ.我对 Tomcat 9 和 IntelliJ 也有同样的问题。 When deployed via IntelliJ it all would work, but deploying that same.war coming from project/target/project.war to tomcat by copying it into $CATALINA_HOME/webapps would result in a successful deployment but with 404 on all my requests.当通过 IntelliJ 部署时,它一切都会工作,但是通过将来自project/target/project.war的 same.war 复制到$CATALINA_HOME/webapps来部署 same.war 到 tomcat 将导致部署成功,但我的所有请求都是 404。

It was just a simple hello world restful app using jersey and my /api/hello-world never worked.它只是一个简单的 hello world restful 应用程序,使用 jersey 而我的 /api/hello-world 从未工作过。 I need to keep both java versions on my machine, so IntelliJ was using 11 and the rest of the system was using 8.我需要在我的机器上保留两个 java 版本,所以 IntelliJ 使用 11 而系统的 rest 使用 8。

All working now.现在都在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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