繁体   English   中英

如何在嵌入式Tomcat上部署Web应用程序 - JavaEE

[英]How to deploy web app on embbeded Tomcat - JavaEE

我想在tomcat embbeded服务器上部署一个web应用程序,我从maven加载:我的休息端点:

@Path("/test")
public class RestTestController {

@GET
public String doNothing(){
    return "A";
}
}

和maven设置:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>8080</port>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

执行mvn clean install之后mvn tomcat7:run我收到war tomcat启动的消息,但是当我输入localhost:8080 / test时我不断收到404 Not Fount Http响应。 我可能做错了什么? 我应该在web.xml中添加某种映射吗?

登录tomcat显示我当然部署了我的应用程序:

[INFO] ---------------------< pl.wachkar:take-restaurant >---------------------
[INFO] Building take-restaurant 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ take-restaurant >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ take-restaurant ---
(...)
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ take-restaurant ---
[INFO] Running war on http://localhost:8080/

@EDIT清空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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">

</web-app>

你的web.xml是什么样的? 您至少需要一个欢迎文件列表,以便tomcat知道在启动时要提供哪个文件

例如:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

我认为你在浏览器中输入了一个错误的URL(可能是不完整的)。如果没有使用web.xml,你应该创建一个扩展Application类的类,并通过@ApplicationPath("anyString")注释它,例如:

  import javax.ws.rs.core.Application;

     @ApplicationPath("myWebservice")
     public class MyConfig extends Application{

    }

你不需要实现任何正常工作的方法。这个类可以在任何包中。 并且用于测试正确的URL是:

localhost:port/contextpath/string1/string2

string1是你在Application annotation中指定的(这里是myWebservice)string2是你在资源类的Path annotation中指定的(在你的case测试中)所以我们有

localhost:8080/contextpath/myWebservice/test

暂无
暂无

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

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