简体   繁体   English

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

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

I want to deploy a web app on tomcat embbeded server which i load from maven: My rest endpoint: 我想在tomcat embbeded服务器上部署一个web应用程序,我从maven加载:我的休息端点:

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

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

And maven setup: 和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>

After performing mvn clean install and later mvn tomcat7:run I recieve message that war tomcat was started, however when i enter localhost:8080/test i constantly recieve 404 Not Fount Http response. 执行mvn clean install之后mvn tomcat7:run我收到war tomcat启动的消息,但是当我输入localhost:8080 / test时我不断收到404 Not Fount Http响应。 What am I possibly doing wrong? 我可能做错了什么? Should I add some kind of mapping into web.xml? 我应该在web.xml中添加某种映射吗?

Logs on tomcat shows that I certainly deployed my app: 登录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 Empty web.xml file: @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>

What does your web.xml look like? 你的web.xml是什么样的? You will need at least a welcome file list so that tomcat knows which file to serve at startup 您至少需要一个欢迎文件列表,以便tomcat知道在启动时要提供哪个文件

For example: 例如:

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

i think you entered a wrong url in the brower(maybe incomplete).without using a web.xml you should create a class that extends Application class and annotate it by @ApplicationPath("anyString") for example: 我认为你在浏览器中输入了一个错误的URL(可能是不完整的)。如果没有使用web.xml,你应该创建一个扩展Application类的类,并通过@ApplicationPath("anyString")注释它,例如:

  import javax.ws.rs.core.Application;

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

    }

you don't need to implement any method it works fine.this class can be in any package. 你不需要实现任何正常工作的方法。这个类可以在任何包中。 and for testing the right url is: 并且用于测试正确的URL是:

localhost:port/contextpath/string1/string2

string1 is what you specify in Application annotation (here myWebservice) string2 is what you specify in Path annotation on your resource class(in your case test) so we have 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