📜 ⬆️ ⬇️

Java REST service is just

Java programmers can seem monstrous and difficult to understand for many programmers. In this small article I would like to show that if you wish, you can build an application from fairly simple components, without resorting to mega-frameworks.



As an example, I chose a simple REST service. Jersey will be used to describe resources. As a bonus, the use of the Dependency Injection framework of Google Guice will be shown. It would have been possible without him, but I don’t want the example to seem too playful and detached from life. I will try to put the whole example in about 50 lines in one file and not a single line of XML will be used.
')
So, let's go:

1. We describe a simple class that will provide access to some information. Let it be a call counter:
@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 .


Annotation Singleton is needed to indicate to Juice that the object should be a singleton :)

2. We describe the service that will return us something, simultaneously tugging counter:
@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. Now we will be friends with Jersey and Guice. I used the ready integration, it is called jersey-guice. The integration is done through a GuiceContainer servlet / filter, to use which you need to declare a ServletModule from the guice-servlet-module extension and specify that the requests we need will be processed by GuiceContainer, which will allow you to declare Jersey resources in the context of Guice.

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 .


In the same place we zabaindili Counter and Resource.

4. It remains to run all this stuff using a servlet container. To do this, we absolutely do not need to collect war-ku and deploy to some Tomcat. You can use the built-in container. Jetty and Grizzly come to mind. I chose the latter. Here is the code that starts the server:

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 .


Note that you had to declare an empty servlet:

@SuppressWarnings( "serial" )
public static class DummySevlet extends HttpServlet { }


* This source code was highlighted with Source Code Highlighter .


You need it so that the Guice filter works. If there is no servlet, Grizzly will not pass the request to any filters.

That's probably all. The following is all the code:

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: Sources

Source: https://habr.com/ru/post/115718/


All Articles