REST web service with good old Jersey example.

Opublikowany
Komentarze 0

REST become web service standard some time ago. To be honest I do not believe that we need SOAP any more since all needed communication functionalities could be done in REST. Probably there are some cases when SOAP web services fits better than REST web services, but generally when I need web services I start with REST.

One of old but still efficient framework for creation of REST web services is Jersey [ Link ] .
So what is Jersey? From the documentation we could see:
‘Jersey is a Java based framework that help java developers creating RESTful Web services with variety of media types and abstract away the low-level details of the client – server communication.’ Well that sounds good for me. I treat it as framework for helping expose REST without many complicated configuration features. That’s really important if You would like to create something universal and easy to understand without additional useless technology stack.

One of nice features that convince me to use Jersey is easy integration with Jetty server.

Basic integration looks like that

public static void main(final String… args) throws Exception {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath(„/”);

Server server = new Server(8080); //server.setHandler(new IndexHandler()); server.setHandler(context); ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, „/*”); jerseyServlet.setInitOrder(0); jerseyServlet.setInitParameter(„jersey.config.server.provider.classnames”, SampleResource.class.getCanonicalName()); server.start(); server.join(); }

For my project [ GitHub Link ] I used dependencies

def versions = [
        «jersey»: ’2.22.2»,
        «jetty» : ’9.3.7.v20160115»
]

repositories { mavenCentral()
}

dependencies { compile «javax.ws.rs:javax.ws.rs-api:2.0.1» compile «javax.servlet:javax.servlet-api:3.1.0»

compile „org.eclipse.jetty:jetty-server:${versions.jetty}” compile „org.eclipse.jetty:jetty-servlet:${versions.jetty}” compile „org.eclipse.jetty:jetty-servlets:${versions.jetty}” compile „org.glassfish.jersey.core:jersey-server:${versions.jersey}” compile „org.glassfish.jersey.core:jersey-client:${versions.jersey}” compile „org.glassfish.jersey.media:jersey-media-json-jackson:${versions.jersey}” compile „org.glassfish.jersey.containers:jersey-container-servlet:${versions.jersey}” testCompile group: «junit», name: «junit», version: «4.11» runtime «javax.servlet:javax.servlet-api:3.0.1» runtime «javax.ws.rs:javax.ws.rs-api:2.0.1» runtime „org.glassfish.jersey.core:jersey-client:${versions.jersey}” runtime „org.glassfish.jersey.core:jersey-server:${versions.jersey}” runtime „org.glassfish.jersey.containers:jersey-container-servlet:${versions.jersey}” runtime „org.glassfish.jersey.media:jersey-media-json-jackson:${versions.jersey}”

}

Please remember about runtime section, especial servlet api is needed.

What really nice is that Jersey is still developed and last release was in Feb 2017.

In my example You could navigate to http://127.0.0.1:8080/sample/hello and see

{"reasult":"hello"}
result.

Autor
Kategorie DSP2017, Java

Komentarze

Nie ma żadnych komentarzy do tego artykułu.

Komentarze

Wpisz swój komentarz. Pola zaznaczone gwiazdką * są wymagane. Przed wysłaniem komentarza musisz zobaczyć podgląd.





Komentarze

Nie ma żadnych komentarzy do tego artykułu.

Komentarze

Wpisz swój komentarz. Pola zaznaczone gwiazdką * są wymagane. Przed wysłaniem komentarza musisz zobaczyć podgląd.