JAX-RSアプリケーションをJava SE環境にデプロイするには、RuntimeDelegate
と、JAX-RS実装でサポートされているHTTPサーバーを使用できます。サーブレットコンテナは必要ありません。
JSR 339状態は次のJava SE環境で
エンドポイントクラスの構成インスタンスがRuntimeDelegate
のcreateEndpoint
方法を用いて得ることができます。アプリケーションは、Application
のインスタンスと必要なエンドポイントのタイプを提供します。実装は、任意のタイプのゼロまたはそれ以上のエンドポイントタイプをサポートしてもよい(MAY)。
結果のエンドポイントクラスインスタンスを使用してアプリケーションを公開する方法は、この仕様の範囲外です。
JAX-RSのリファレンス実装であるJerseyは、Java SEでJAX-RSアプリケーションを展開するために使用できるrange of HTTP serversをサポートしています。
例えば、GrizzlyとRuntimeDelegate
では、次のことができます。
public class Example {
public static void main(String[] args) {
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(GreetingsResource.class);
HttpHandler handler = RuntimeDelegate.getInstance()
.createEndpoint(resourceConfig, HttpHandler.class);
HttpServer server = HttpServer.createSimpleServer(null, 8080);
server.getServerConfiguration().addHttpHandler(handler);
try {
server.start();
System.out.println("Press any key to stop the server...");
System.in.read();
} catch (Exception e) {
System.err.println(e);
}
}
@Path("/greetings")
public static class GreetingsResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getGreeting(){
return "Hello from the other side.";
}
}
}
アプリケーションがhttp://localhost:8080/greetings
で利用できるようになります。
次の依存関係は、上記の例のために必要とされる:
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.25.1</version>
</dependency>
他のサポートの実装は、次のとおり
Jersey documentationには、RuntimeDelegate
のないJava SE環境用の他の配置方法も記載されています。
私はtomcat glassfishなどを使用しないことをお勧めします。ednpoint – Andrey