【问题标题】:How to use jersey with Grizzly 2.3如何在 Grizzly 2.3 中使用球衣
【发布时间】:2013-10-20 14:03:38
【问题描述】:

我目前正在试用 Grizzly-Framework 2.3.6。 我正在使用以下 Maven 依赖项:

    <dependency>
        <groupId>org.glassfish.grizzly</groupId>
        <artifactId>grizzly-framework</artifactId>
        <version>2.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.grizzly</groupId>
        <artifactId>grizzly-http-server</artifactId>
        <version>2.3.6</version>
    </dependency>

我可以使用以下代码示例启动服务器:

HttpServer server = HttpServer.createSimpleServer();
try {
    server.start();
    addJaxRS(server);
    System.out.println("Press any key to stop the server...");
    System.in.read();
} catch (Exception e) {
    System.err.println(e);
} 

我添加了以下 JAX-RS 类:

@Path("/helloworld")
public class HelloWorldResource {
    @GET 
    @Produces("text/plain")
    public String getClichedMessage() {
        return "Hello World";
    }
}

我的问题是:如何告诉 grizzly 将 HelloWorldRessoruce 添加为 JAX-RS 资源?

【问题讨论】:

    标签: java jersey grizzly


    【解决方案1】:

    我通过将依赖项更改为“jersey-grizzly2”找到了解决方案,其中包括 grizzly 版本 2.2.16

    <dependencies>
            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-grizzly2</artifactId>
                <version>1.17.1</version>
            </dependency>
    </dependencies>
    

    我现在可以像这样开始使用我的 JAX-RS 资源:

    import java.io.IOException;
    import org.glassfish.grizzly.http.server.HttpServer;
    import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
    import com.sun.jersey.api.core.PackagesResourceConfig;
    import com.sun.jersey.api.core.ResourceConfig;
    public class Main {
        public static void main(String[] args) throws IOException {
            // HttpServer server = HttpServer.createSimpleServer();
            // create jersey-grizzly server
            ResourceConfig rc = new PackagesResourceConfig("my.resources");
            HttpServer server = GrizzlyServerFactory.createHttpServer(
                    "http://localhost:8080", rc);
            try {
                server.start();
                System.out.println("Press any key to stop the server...");
                System.in.read();
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    }
    

    但我最初认为球衣是灰熊的一部分?

    【讨论】:

    • 不,Grizzly 和 Jersey 是独立的项目。
    • 在“my.resources”中放置了哪些选项?
    • 在 my.resources 中,我使用 jax-rs 注释添加了我的休息服务类。你可以在这里看到代码:github.com/rsoika/ImixsCrypt/tree/master/imixs-crypt-privacy/…
    • 当您没有将布尔值传递给 createHttpServer() 时,它将在创建服务器后尝试启动服务器。您不需要调用 server.start()。
    猜你喜欢
    • 2017-10-02
    • 2017-03-21
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2017-12-17
    • 2016-01-03
    相关资源
    最近更新 更多