带有 Jersey 或 resteasy 的嵌入式码头 [英] Embedded jetty with Jersey or resteasy

查看:38
本文介绍了带有 Jersey 或 resteasy 的嵌入式码头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有 JAX-RS(resteasy 或 jersey)的嵌入式码头制作 RESTful 服务.我正在尝试使用 maven/eclipse 设置进行创建.如果我尝试关注 http://wikis.sun.com/pages/viewpage.action?pageId=21725365 链接我无法解决来自 ServletHolder sh = new ServletHolder(ServletContainer.class);

I want make RESTful services using embedded jetty with JAX-RS (either resteasy or jersey). I am trying to create with maven/eclipse setup. if I try to follow http://wikis.sun.com/pages/viewpage.action?pageId=21725365 link I am not able to resolve error from ServletHolder sh = new ServletHolder(ServletContainer.class);

public class Main {

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

        @GET
        public String get() {
            return "GET";
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        ServletHolder sh = new ServletHolder(ServletContainer.class);

        /*
         * For 0.8 and later the "com.sun.ws.rest" namespace has been renamed to
         * "com.sun.jersey". For 0.7 or early use the commented out code instead
         */
        // sh.setInitParameter("com.sun.ws.rest.config.property.resourceConfigClass",
        // "com.sun.ws.rest.api.core.PackagesResourceConfig");
        // sh.setInitParameter("com.sun.ws.rest.config.property.packages",
        // "jetty");
        sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
            "com.sun.jersey.api.core.PackagesResourceConfig");
        sh.setInitParameter("com.sun.jersey.config.property.packages",
            "edu.mit.senseable.livesingapore.platform.restws");
        // sh.setInitParameter("com.sun.jersey.config.property.packages",
        // "jetty");
        Server server = new Server(9999);

        ServletContextHandler context = new ServletContextHandler(server, "/",
            ServletContextHandler.SESSIONS);
        context.addServlet(sh, "/*");
        server.start();
        server.join();
        // Client c = Client.create();
        // WebResource r = c.resource("http://localhost:9999/");
        // System.out.println(r.get(String.class));
        //
        // server.stop();
    }
}

即使这样也行不通.谁能给我一些建议/教程/示例?

even this is not working. can anyone suggest me something/tutorial/example ?

推荐答案

嗯,链接页面很古老——上次更新是 3 年前.

huh, linked page is ancient - last update 3 years ago.

你真的需要码头吗?Jersey 与 Grizzly 具有经过全面测试的出色集成(请参阅 http://grizzly.java.net),它也充当 Glassfish 传输层,它可以像在您的示例中一样使用它.

Do you really need jetty? Jersey has excellent thoroughly tested integration with Grizzly (see http://grizzly.java.net) which is also acting as Glassfish transport layer and it is possible to use it as in your example.

从 Jersey 工作区查看 helloworld 示例,com.sun.jersey.samples.helloworld.Main 类启动 Grizzly 并部署"helloworld 应用程序:http://repo1.maven.org/maven2/com/sun/jersey/samples/helloworld/1.9.1/helloworld-1.9.1-project.zip .

See helloworld sample from Jersey workspace, com.sun.jersey.samples.helloworld.Main class starts Grizzly and "deploys" helloworld app: http://repo1.maven.org/maven2/com/sun/jersey/samples/helloworld/1.9.1/helloworld-1.9.1-project.zip .

如果您真的需要基于码头的样品,我想我应该能够提供(请随时与我联系).

If you really need jetty based sample, I guess I should be able to provide it (feel free to contact me).

好的,如果你真的想要码头,你可以拥有它 :) 并且看起来相当简单.我按照 http://docs.codehaus.org/display/JETTY/Embedding+Jetty 和能够启动 helloworld 示例:

ok, if you really want jetty, you can have it :) and looks like its fairly simple. I followed instructions from http://docs.codehaus.org/display/JETTY/Embedding+Jetty and was able to start helloworld sample:

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    Context root = new Context(server,"/",Context.SESSIONS);
    root.addServlet(new ServletHolder(new ServletContainer(new PackagesResourceConfig("com.sun.jersey.samples.helloworld"))), "/");
    server.start();
}

http://localhost:8080/helloworld 是可访问的.我使用了 Jetty 6.1.16.希望对您有所帮助!

http://localhost:8080/helloworld is accessible. I used Jetty 6.1.16. Hope it helps!

您可以在用户指南中找到有关在 servlet 环境中配置 Jersey 的更多信息,请参阅 http://jersey.java.net/nonav/documentation/latest/

You can find more information about configuring Jersey in servlet environment in user guide, see http://jersey.java.net/nonav/documentation/latest/

依赖关系..但这有点难以指定,它最近在球衣中发生了变化..所以..

dependencies.. but this is kind of hard to specify, it changed recently in jersey.. so..

1.10 之前:

<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty</artifactId>
    <version>6.1.16</version>
</dependency>
<dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-server</artifactId>
     <version>${jersey.version}</version>
</dependency>

1.10 后:

<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty</artifactId>
    <version>6.1.16</version>
</dependency>
<dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-servlet</artifactId>
     <version>${jersey.version}</version>
</dependency>

你需要这个用于码头的 maven repo:

and you need this maven repo for jetty:

<repositories>
    <repository>
        <id>codehaus-release-repo</id>
        <name>Codehaus Release Repo</name>
        <url>http://repository.codehaus.org</url>
    </repository>
</repositories>

这篇关于带有 Jersey 或 resteasy 的嵌入式码头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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