我一直试图关注这个example(首次点击google for jersey + jetty),但运气不佳。
根据以下评论者的建议,我决定更新示例以使用最新版本的Jetty(9.1.0.RC2)和Jersey(2.7)。
以下是更新的pom,其中包含更新的依赖项:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HelloJerseyLatest</groupId>
<artifactId>HelloJerseyLatest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.1.0.RC2</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.1.0.RC2</version>
</dependency>
</dependencies>
</project>
首先要注意的是,Jersey套餐已从com.sun.jersey.spi.*
更改为org.glassfish.jersey.*
。
因此,主要方法也需要更改:
package example.server;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
//import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletContainer;
public class MinimalServerRest {
public static void main(String[] args) throws Exception {
ServletHolder sh = new ServletHolder(ServletContainer.class);
// these initialization strings will need to be updated.
sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
sh.setInitParameter("com.sun.jersey.config.property.packages", "rest");//Set the package where the services reside
sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
Server server = new Server(9999);
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
context.addServlet(sh, "/*");
server.start();
server.join();
}
}
将原始示例代码更新为最新需要进行哪些更改?我没有web.xml文件。我需要一个吗?
答案 0 :(得分:11)
我意识到这不是你给工作的例子(你的示例链接坏了) - 我不太了解Jersey 1,并且试图升级其他人的项目很困难。当你有另一个question要求HelloWorld
例子的时候,我认为你只需要一些东西让你自己去看泽西&amp;码头。
所以,你去 - 2个例子,一个使用JettyHttpContainerFactory
,另一个使用Jersey
ServletContainer
。
首先是泽西资源 - 真的很简单。这会为类设置"test"
路径,并设置一个路径为hello
的方法,接受GET
,以纯文本格式生成“Hello World”。
@Path("/test")
public class TestResource {
@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello World";
}
}
接下来是服务器类:
public class ExampleServer {
public static void main(String[] args) {
URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
ResourceConfig config = new ResourceConfig(TestResource.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
}
}
最后是pom依赖项(注意这两个例子都有依赖关系)。
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.1.3.v20140225</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.1.3.v20140225</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
<!-- if you want to enable JSON support, include Moxy and Jersey will automatically enable the Feature -->
</dependencies>
另请参阅https://jersey.java.net/apidocs/2.7/jersey/javax/ws/rs/core/Feature.html了解功能 - 通过在类路径中包含Moxy,Jersey将自动注册MoxyJSONFeature。如果您更愿意使用Jackson,则需要手动注册JacksonFeature以及依赖项。您可以在注册资源(逗号分隔)
的同一初始化参数中注册任何功能如果您希望配置为servlet,请将其用作ExampleServer代码
public class ExampleServer {
public static void main(String[] args) throws Exception {
Server server = new Server(9998);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
/*This parameter tells the Jersey Servlet which of your REST resources to load. In this example we're adding the TestResource class. Jersey will then invoke this class for requests coming into paths denoted by the @Path parameter within the TestResource class. If you have multiple classes, you can either list them all comma separated, of use "jersey.config.server.provider.packages" and list the package name instead */
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "foo.bar.TestResource");
server.start();
server.join();
}
}
请注意servlet版本,我正在定义资源的类名。如果您有一些,最好使用jersey.config.server.provider.packages
希望这会有所帮助。如果您有任何问题,请告诉我。
威尔