有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我一直在尝试跟踪这个example(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();
      }
}

要将原始示例代码更新为当前代码,需要进行哪些更改?我没有网络。xml文件。我需要吗

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    我意识到这并没有让你给出的例子发挥作用(你的例子链接断了)-我对Jersey 1不太了解,尝试升级其他人的项目很困难。因为你有另一个question要求一个HelloWorld的例子,我想你只是需要一些东西来让自己与泽西&;码头

    这里有两个例子,一个使用JettyHttpContainerFactory,另一个使用Jersey{}

    首先是泽西岛资源——非常简单。这将设置路径为"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,请将其用作示例服务器代码

    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提供包名

    希望这有帮助。如果你有任何问题,请告诉我

    威尔