我目前正在试用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资源?
答案 0 :(得分:6)
我通过将依赖项更改为“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);
}
}
}
但我最初认为球衣是Grizzly的一部分?