Java RESTサービスは簡単です

多くのプログラマーにとって、Javaテクノロジーは巨大で理解しにくいと思われるかもしれません。 この短い記事では、必要に応じて、メガフレームワークに頼らずにかなり単純なコンポーネントからアプリケーションを構築できることを示したいと思います。







例として、単純なRESTサービスを選択しました。 ジャージーは、リソースの説明に使用されます。 ボーナスとして、Google GuiceフレームワークのDependency Injectionフレームワークの使用が示されます。 彼がいなくても可能だろうが、私はこの例があまりにもおもちゃっぽく、人生から離婚したように思われたくない。 例全体を1つのファイルの約50行に収めようとし、1行のXMLは使用しません。



それでは、行きましょう:



1.いくつかの情報へのアクセスを提供する単純なクラスについて説明します。 それを呼び出しカウンターにしましょう:

@Singleton

public static class Counter {

private final AtomicInteger counter = new AtomicInteger(0);

public int getNext() {

return counter.incrementAndGet();

}

}




* This source code was highlighted with Source Code Highlighter .








シングルトン注釈は、オブジェクトがシングルトンであることをjuisに伝えるために必要です:)



2.何かを返し、同時にカウンターをプルするサービスについて説明します。

@Path( "/hello" )

public static class Resource {



@Inject Counter counter;



@GET

public String get () {

return "Hello, User number " + counter.getNext();

}

}




* This source code was highlighted with Source Code Highlighter .








3.ジャージーとギスと友達になりましょう。 私はすぐに利用できる統合を利用しました。これは、ジャージー・ゲストと呼ばれます。 統合はサービス/フィルターGuiceContainerを介して実行されます。このためには、guice-servlet-module拡張からServletModuleを宣言し、必要な要求がGuiceContainerによって処理されることを示す必要があります。



public static class Config extends GuiceServletContextListener {

@Override

protected Injector getInjector() {

return Guice.createInjector( new ServletModule(){

@Override

protected void configureServlets() {

bind(Resource. class );

bind(Counter. class );

serve( "*" ).with(GuiceContainer. class );

}

});

}

}




* This source code was highlighted with Source Code Highlighter .








同じ場所で、カウンターとリソースをバウンスしました。



4.サーブレットコンテナを使用してこれらすべてを実行することは残ります。 これを行うために、war-kuを収集してTomcatにデプロイする必要はありません。 埋め込みコンテナを使用できます。 JettyとGrizzlyが思い浮かびます。 最後のものを選びました。 サーバーを起動するコードは次のとおりです。



public static void main( String [] args) throws Exception {

int port = Integer.valueOf(System.getProperty( "port" ));

GrizzlyWebServer server = new GrizzlyWebServer(port);

ServletAdapter adapter = new ServletAdapter( new DummySevlet());

adapter.addServletListener(Config. class .getName());

adapter.addFilter( new GuiceFilter(), "GuiceFilter" , null );

server.addGrizzlyAdapter(adapter, new String []{ "/" });

server.start();

}




* This source code was highlighted with Source Code Highlighter .








空のサーブレットを宣言する必要があることに注意してください。



@SuppressWarnings( "serial" )

public static class DummySevlet extends HttpServlet { }




* This source code was highlighted with Source Code Highlighter .








Guiceフィルターが機能するために必要です。 サーブレットがない場合、Grizzlyはリクエストをフィルターに渡しません。



それがおそらくすべてです。 次に、コード全体を示します。



public class App {



@Path( "/hello" )

public static class Resource {



@Inject Counter counter;



@GET

public String get () {

return "Hello, User number " + counter.getNext();

}

}



@Singleton

public static class Counter {

private final AtomicInteger counter = new AtomicInteger(0);

public int getNext() {

return counter.incrementAndGet();

}

}



public static class Config extends GuiceServletContextListener {

@Override

protected Injector getInjector() {

return Guice.createInjector( new ServletModule(){

@Override

protected void configureServlets() {

bind(Resource. class );

bind(Counter. class );

serve( "*" ).with(GuiceContainer. class );

}

});

}

}



@SuppressWarnings( "serial" )

public static class DummySevlet extends HttpServlet { }



public static void main( String [] args) throws Exception {

int port = Integer.valueOf(System.getProperty( "port" ));

GrizzlyWebServer server = new GrizzlyWebServer(port);

ServletAdapter adapter = new ServletAdapter( new DummySevlet());

adapter.addServletListener(Config. class .getName());

adapter.addFilter( new GuiceFilter(), "GuiceFilter" , null );

server.addGrizzlyAdapter(adapter, new String []{ "/" });

server.start();

}

}




* This source code was highlighted with Source Code Highlighter .








UPD: ソース



All Articles