如何更新示例以使用最新版本的 Jetty (9.1.0.RC2) 和 Jersey (2.7)? [英] How do I update example to work with latest versions Jetty (9.1.0.RC2) and Jersey (2.7)?

查看:34
本文介绍了如何更新示例以使用最新版本的 Jetty (9.1.0.RC2) 和 Jersey (2.7)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力关注这个 要求提供 HelloWorld 示例,我假设您只是需要一些东西来获得你自己和泽西岛一起去码头.

这里有 2 个示例,一个使用 JettyHttpContainerFactory,另一个使用 Jersey ServletContainer.

首先是泽西资源 - 非常简单.这设置了一个路径为 "test" 的类,以及一个路径为 hello 的方法,接受生成Hello World"的 GET纯文本.

@Path("/test")公共类TestResource {@得到@Path("你好")@Produces(MediaType.TEXT_PLAIN)公共字符串你好(){返回你好世界";}}

接下来是服务器类:

公共类 ExampleServer {公共静态无效主要(字符串[]参数){URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();ResourceConfig config = new ResourceConfig(TestResource.class);服务器服务器 = JettyHttpContainerFactory.createServer(baseUri, config);}}

最后是 pom 依赖项(请注意,这两个示例都有依赖项).

 <依赖><依赖性><groupId>org.eclipse.jetty</groupId><artifactId>码头服务器</artifactId><版本>9.1.3.v20140225</版本></依赖><依赖性><groupId>org.eclipse.jetty</groupId><artifactId>jetty-servlet</artifactId><版本>9.1.3.v20140225</版本></依赖><依赖性><groupId>org.glassfish.jersey.core</groupId><artifactId>球衣服务器</artifactId><版本>2.7</版本></依赖><依赖性><groupId>org.glassfish.jersey.containers</groupId><artifactId>jersey-container-servlet-core</artifactId><版本>2.7</版本></依赖><依赖性><groupId>org.glassfish.jersey.containers</groupId><artifactId>jersey-container-jetty-http</artifactId><版本>2.7</版本></依赖><依赖性><groupId>org.glassfish.jersey.media</groupId><artifactId>jersey-media-moxy</artifactId><版本>2.7</版本></依赖><!-- 如果要启用 JSON 支持,包含 Moxy 和 Jersey 会自动启用 Feature --></依赖关系>

另见 https://jersey.java.net/apidocs/2.7/jersey/javax/ws/rs/core/Feature.html 了解功能 - 通过在类路径中包含 Moxy,Jersey 将自动注册 MoxyJSONFeature.如果您更愿意使用 Jackson,则需要手动注册 JacksonFeature 以及依赖项.您可以在与注册资源相同的 init 参数中注册任何功能(逗号分隔)

如果您希望配置为 servlet,请将其用作 ExampleServer 代码

公共类 ExampleServer {公共静态 void main(String[] args) 抛出异常 {服务器服务器=新服务器(9998);ServletContextHandler 上下文 = 新的 ServletContextHandler(ServletContextHandler.SESSIONS);context.setContextPath("/");server.setHandler(上下文);ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");jerseyServlet.setInitOrder(0);/*此参数告诉 Jersey Servlet 要加载哪些 REST 资源.在此示例中,我们添加了 TestResource 类.Jersey 将为进入 TestResource 类中 @Path 参数表示的路径的请求调用此类.如果您有多个类,则可以将它们全部以逗号分隔列出,或者使用jersey.config.server.provider.packages"并列出包名称 */jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "foo.bar.TestResource");server.start();server.join();}}

注意 servlet 版本,我正在定义资源的类名.如果您有几个,最好使用 jersey.config.server.provider.packages

来提供包名称

希望这会有所帮助.如果您有任何问题,请告诉我.

I've been trying to follow this example (first hit on google for jersey + jetty) but with not much luck.

At the suggestion of the commenters below, I've decided to update the example to use the latest versions of Jetty (9.1.0.RC2) and Jersey (2.7).

Here is the updated pom with updated dependencies:

<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>

The first thing to notice is that Jersey packages have changed from com.sun.jersey.spi.* to org.glassfish.jersey.*. As a result, the main method will need to be changed as well:

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();
      }
}

what changes will be needed to update the original sample code to be current? I don't have a web.xml file. Do I need one?

解决方案

I realise that this isn't getting the example you gave to work (your example link is broken) - I don't know Jersey 1 very well, and trying to upgrade someone else's project is difficult. As you have another question asking for a HelloWorld example, I assume you're just needing something to get yourself going with Jersey & Jetty.

So here you go - 2 examples, one using the JettyHttpContainerFactory and the other using the Jersey ServletContainer.

First the Jersey Resource - really simple. This sets the class with a path of "test", and one method with a path of hello, accepting GET which produces "Hello World" in plain text.

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

    @GET
    @Path("hello")
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
       return "Hello World";
    }
}

Next the server class:

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);
       }
}

And finally the pom dependencies (note there are dependencies here for both examples).

       <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>

Also see https://jersey.java.net/apidocs/2.7/jersey/javax/ws/rs/core/Feature.html for an understanding of Features - by including Moxy on the classpath, Jersey will automatically register the MoxyJSONFeature. If you'd rather use Jackson, you'll need to manually register the JacksonFeature, as well as the dependency. You can register any feature, in the same init param as registering your resources (comma separated)

If you would prefer to configure as a servlet use this as the ExampleServer code

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();
       }
}

Note with the servlet version, I'm defining the class name of my resource. If you have a few it's best to provide the package name instead using jersey.config.server.provider.packages

Hope this helps. Let me know if you have any problems.

Will

这篇关于如何更新示例以使用最新版本的 Jetty (9.1.0.RC2) 和 Jersey (2.7)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆