我有一棵看起来像这样的树:
在 java 文件夹中,我有一个包 com.example.project
,它有一个 Main
类和一个子类化的 Application
类资源配置
@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中的目录是project
,javax.ws.rs.ApplicationPath
是api
,@Path
是服务
。因此,例如,我希望从 /project/api/service
得到响应。但是,在尝试了很多组合之后,一切都是 404。
最佳答案
您需要将 jersey-servlet.jar
添加到您的 war 文件中。
在 pom.xml
中,它可能看起来像这样:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
关于java - 将 Jersey 应用程序部署到 Tomcat - 资源 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25875947/