【问题标题】:How to deploy web app on embbeded Tomcat - JavaEE如何在嵌入式 Tomcat 上部署 Web 应用程序 - JavaEE
【发布时间】:2019-08-11 15:53:17
【问题描述】:

我想在我从 maven 加载的 tomcat 嵌入式服务器上部署一个 Web 应用程序: 我的休息端点:

@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/

@编辑 空 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>

【问题讨论】:

  • Tomcat 不支持开箱即用的 JAX-RS。您需要将 JAX-RS 实现集成到您的应用程序中。我不相信这时候可以用一个空的 web.xml 文件来完成。

标签: tomcat jakarta-ee web-applications


【解决方案1】:

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

例如:

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

【讨论】:

  • 我的 web.xml 完全为空。我认为在maven依赖中设置路径就足够了
  • 但这不是我想要达到的。我只想设置 api,而不是页面
【解决方案2】:

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

  import javax.ws.rs.core.Application;

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

    }

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

localhost:port/contextpath/string1/string2

string1 是您在应用程序注释中指定的(此处为 myWebservice) string2 是您在资源类的路径注释中指定的(在您的案例测试中)所以我们有

localhost:8080/contextpath/myWebservice/test

【讨论】:

    猜你喜欢
    • 2011-01-14
    • 1970-01-01
    • 2019-05-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    相关资源
    最近更新 更多