【问题标题】:Deploy Jersey Application to Tomcat - Resources 404将 Jersey 应用程序部署到 Tomcat - 资源 404
【发布时间】:2014-09-16 18:26:12
【问题描述】:

我有一棵看起来像这样的树:

在 java 文件夹中,我有一个包 com.example.project,它有一个 Main 类和一个子类 ResourceConfigApplication

@ApplicationPath("api")
public class Application extends ResourceConfig {
    public Application() {
        packages("com.example.project");
        property(MustacheMvcFeature.TEMPLATE_BASE_PATH, "templates");
        register(MustacheMvcFeature.class);
        register(MyRequestFilter.class);
    }
}

在开发中,我运行 Main 类并启动一个 Grizzly 服务器:

public class Main {

    public static HttpServer startServer(String BASE_URI) {
        final Application app = new Application();
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), app);
    }

    public static void main(String[] args) throws IOException {
        String BASE_URI = "http://localhost:8080/api";
        final HttpServer server = startServer(BASE_URI);

        server.getServerConfiguration().addHttpHandler(
                new StaticHttpHandler("src/main/webapp"), "/");

        System.out.println(String.format(
                "Jersey app started with WADL available at "
                + "%s/application.wadl\nHit enter to stop it...", BASE_URI)
        );

        System.in.read();
    }
}

Grizzly 服务器一切正常。我现在正在尝试部署到 tomcat。

我使用mvn package 创建一个war 文件。在我的 pom 文件中,我有:

<build>
    ...
        <plugins>
        ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

我将war文件放在tomcat实例下的webapps目录中,它在那里爆炸。我的静态内容已投放。

来自 WAR 文件的目录布局如下所示:

[vagrant@vagrant-centos6 project]$ tree .
.
├── index.html
| ... <webapp content>
├── META-INF
│   ├── MANIFEST.MF
│   └── maven
│       └── com.example.project
│           └── project
│               ├── pom.properties
│               └── pom.xml
└── WEB-INF
    ├── classes
    │   ├── com
    │   │   └── example
    │   │       └── project
    │   │           ├── Application.class
    │   │           ├── Main.class
    |   |           ...
    │   ├── filter-dev.properties
    │   ├── filter-test.properties
    │   ├── hibernate.cfg.xml
    │   └── templates
    │       └── foo.mustache
    └── lib
       ...

我找不到我的任何 REST 服务。我尝试的一切都是 404。

tomcat webapps中的目录是projectjavax.ws.rs.ApplicationPathapi@Pathservice。因此,例如,我希望得到/project/api/service 的回复。但是,尝试了很多组合后,一切都是404。

【问题讨论】:

    标签: java maven tomcat jersey jax-rs


    【解决方案1】:

    您需要将jersey-servlet.jar 添加到您的war 文件中。

    pom.xml 中,可能如下所示:

       <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2017-04-20
      • 2017-01-04
      • 1970-01-01
      • 2013-05-14
      相关资源
      最近更新 更多