rss*_*919 13 java jersey-2.0 tomcat8
我有Web容器和Tomcat的知识,可以部署静态和动态网站.但我是REST和泽西新手.我已阅读2.6用户指南,查看了许多网站和YouTube视频.在1.x Jersey上似乎有很多信息,但在2.x上没有太多信息我可以在我的环境中使用1.18,但似乎无法使任何部署模型适用于2.x. 我在2.x中注意到有一个应用程序部署模型.所以我想我会问一些非常通用的问题来开始.
谢谢
6ce*_*cef 40
以下是我希望相对完整的解决方案.
你没有提到Maven,所以我会:Maven是你的朋友.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.groupid</groupId>
<artifactId>stack</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
<build>
<finalName>stack</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<name>Stack</name>
</project>
Run Code Online (Sandbox Code Playgroud)
就依赖性而言,这可能不是绝对最小值,但它很接近.
但那只是pom.技巧继续在web.xml和Java类中.
这太疯狂了,所以请耐心等待:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false"
version="3.1">
</web-app>
Run Code Online (Sandbox Code Playgroud)
好吧,也许不那么复杂.
请注意,设置metadata-complete="true"
可能会导致Tomcat启动更快.
一个是"申请",另一个是休息.***
其余的电话非常简单:
package some.package;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/hello")
public class HelloRest {
@GET
public String message() {
return "Hello, rest!";
}
}
Run Code Online (Sandbox Code Playgroud)
该应用程序如下所示:
package some.package;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import some.package.HelloRest;
@ApplicationPath("/rest")
public class RestApp extends Application {
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(HelloRest.class));
}
}
Run Code Online (Sandbox Code Playgroud)
就是这样.当您导航到类似的东西时http://localhost:8080/stack/rest/hello
,您应该看到文本"Hello,rest!"
getClasses()
在RestApp中有点难看.您可以使用Jersey的ResourceConfig,如Jersey用户指南,它看起来像这样:
public class RestApp extends ResourceConfig {
public RestApp() {
packages("some.package");
}
}
Run Code Online (Sandbox Code Playgroud)
精细.这些是Eclipse列为Maven依赖项的jar:
据推测,手动将这些添加到类路径应该可行.或者使用Maven.
我能够使用 Jersey 2.6 用户指南中提供的用于部署到 3.x servlet 容器的说明来完成这项工作。我最终使用了与下面的项目类似的东西。由于 URL 映射是在 .xml 中提供的,因此您可以从 Application 子类中省略 @ApplicationPath。
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- Servlet declaration can be omitted in which case
it would be automatically added by Jersey -->
<servlet>
<servlet-name>org.example.MyApplication</servlet-name>
</servlet>
<!-- Servlet mapping can be omitted in case the Application subclass
is annotated with @ApplicationPath annotation; in such case
the mapping would be automatically added by Jersey -->
<servlet-mapping>
<servlet-name>org.example.MyApplication</servlet-name>
<url-pattern>/myresources/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15739 次 |
最近记录: |