簡體   English   中英

如何在Grizzly 2.3中使用球衣

[英]How to use jersey with Grizzly 2.3

我目前正在嘗試使用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";
    }
}

我的問題是:如何告訴灰熊將HelloWorldRessoruce添加為JAX-RS資源?

我找到了一個解決方案,方法是將依賴項更改為“ jersey-grizzly2”,其中包括灰熊版本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);
        }
    }
}

但是我最初認為球衣是灰熊的一部分嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM