加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何更新示例以使用最新版本Jetty(9.1.0.RC2)和Jersey(2

发布时间:2020-12-15 04:42:51 所属栏目:Java 来源:网络整理
导读:我一直在努力追随这个 example(谷歌首次点击泽西码头),但运气不佳. 根据以下评论者的建议,我决定更新示例以使用最新版本的Jetty(9.1.0.RC2)和Jersey(2.7). 这是更新的pom,具有更新的依赖项: project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="
我一直在努力追随这个 example(谷歌首次点击泽西码头),但运气不佳.

根据以下评论者的建议,我决定更新示例以使用最新版本的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文件.我需要一个吗?

解决方法

我意识到这不是你给工作的例子(你的例子链接坏了) – 我不太了解泽西1,并且试图升级其他人的项目很困难.当你有另一个 question要求一个HelloWorld示例时,我认为你只需要一些东西让你自己去看泽西&码头.

所以这里你Go – 两个例子,一个使用JettyHttpContainerFactory,另一个使用Jersey ServletContainer.

首先是泽西资源 – 非常简单.这将使用“test”路径设置类,使用hello路径设置一个方法,接受以纯文本形式生成“Hello World”的GET.

@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提供软件包名称

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读