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

Java 如何更新示例以使用最新版本Jetty(9.1.0.RC2)和Jersey(2.7)?,java,maven,jersey,jetty,Java,Maven,Jersey,Jetty,我一直在尝试关注这一点(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:/

我一直在尝试关注这一点(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>
要将原始示例代码更新为当前代码,需要进行哪些更改?我没有web.xml文件。我需要一个吗


我意识到这并不能让您给出的示例发挥作用(您的示例链接已断开)-我对Jersey 1不太了解,尝试升级其他人的项目很困难。由于您有另一个要求使用
HelloWorld
示例,我假设您只是需要一些东西来让自己使用Jersey&Jetty

下面是两个示例,一个使用
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依赖项(注意,这两个示例都有依赖项)

注意,在servlet版本中,我定义了资源的类名。如果有一些,最好使用
jersey.config.server.provider.packages提供包名

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


Will

如果您刚刚起步,您应该使用当前版本的Jersey,即2.7。谷歌最热门的游戏给你的球衣是1.18,这可不太好。。。有关如何将Jersey与servlet容器一起使用,以及如何将Jersey与Jetty的HTTP服务器一起使用的信息,请参见使用Jersey 1是否需要
?2个够了吗?我不准备修复Jersey 1示例,但可以提供一个可工作的Jetty/Jersey 2示例。当然可以。我已经升级到2,但又卡住了。早上几小时后我会更新这个问题,我不明白这行的第一个参数:jerseyServlet.setInitParameter(“jersey.config.server.provider.classnames”,“foo.bar.TestResource”);这是什么意思?你能给我指一些我可以阅读更多关于这个的文档吗?完成-查看init参数上面的注释。同样,see看不到您的错误,但是您阅读了文档中的相关JSON部分了吗?您需要包含正确的依赖项,并可能注册一个功能。当我尝试返回带有@products(MediaType.APPLICATION_JSON)的JSON对象时,我得到错误:javax.servlet.ServletException:org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:MessageBodyWriter找不到媒体类型=应用程序/json,类型=类提供者。Employee,genericType=类提供者。Employee。我通过在POM中添加以下内容对其进行了更正:org.glassfish.jersey.media jersey media moxy 2.7这是解决此问题的正确方法吗?如果这是正确的方法,您介意编辑您的答案以将其包含在POM中吗?
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);
       }
}
       <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>
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();
       }
}