Sometime back I’ve written a tutorial on How to build RESTful Service with Java using JAX-RS and Jersey. After all detailed steps you have to deploy your project to Tomcat Web Server
and you should be all good. With that tutorial you should be able to simply create RESTFul service in ~20 mins.
But what about doing it in
~5 mins
? Also sometimes it’s very difficult to setup Tomcat for your web application.
There are number of reasons:
- You may need to have Tomcat binaries copy over to development environment.
- If you don’t have all setup correctly configured then your Tomcat application may not work.
- You must have to export your project as
.war
project - What if you don’t want to deploy your application on Tomcat and run simple .jar?
- With .jar project – how will you start http server? Will your application accepts incoming REST call?
Extra: How to add CORS Filter to your Jersey Web Application?
You must add Cross-origin resource sharing
to your Web Jersey Application. Please follow this tutorial for the same:
What is Cross-Origin Resource Sharing (CORS) – How to add it to your Java Jersey Web Server?
Well, there is a simple solution. There is a way to create a very basic HTTP server (supporting only GET/POST) in Java using just the Java SE API, without writing code to manually parse HTTP requests and manually format HTTP responses.
Using com.sun.net.httpserver.HttpServer
, we should be able to achieve all above.
Let’s get started:
Step 1:
Create new Java project “CrunchifyJerseyEmbeddedHTTPServer
“.
Step 2:
Convert it to Maven Project. This will simplify our life for adding dependencies. Please follow this tutorial to convert existing Java Project to Maven Project.
Step 3:
In this project we just need to one jersey-server
dependency.
Step 4:
Create JerseyEmbeddedHTTPServerCrunchify.java
under com.crunchify.tutorial
package.
package com.crunchify.tutorial; import java.io.IOException; import java.net.InetAddress; import java.net.URI; import java.net.UnknownHostException; import javax.ws.rs.core.UriBuilder; import com.sun.jersey.api.container.httpserver.HttpServerFactory; import com.sun.jersey.api.core.PackagesResourceConfig; import com.sun.jersey.api.core.ResourceConfig; import com.sun.net.httpserver.HttpServer; /** * @author Crunchify.com * */ @SuppressWarnings("restriction") public class JerseyEmbeddedHTTPServerCrunchify { public static void main(String[] args) throws IOException { System.out.println("Starting Crunchify's Embedded Jersey HTTPServer...\n"); HttpServer crunchifyHTTPServer = createHttpServer(); crunchifyHTTPServer.start(); System.out.println(String.format("\nJersey Application Server started with WADL available at " + "%sapplication.wadl\n", getCrunchifyURI())); System.out.println("Started Crunchify's Embedded Jersey HTTPServer Successfully !!!"); } private static HttpServer createHttpServer() throws IOException { ResourceConfig crunchifyResourceConfig = new PackagesResourceConfig("com.crunchify.tutorial"); // This tutorial required and then enable below line: https://crunchify.com/?p=6360 //crunchifyResourceConfig.getContainerResponseFilters().add(CrunchifyCORSFilter.class); return HttpServerFactory.create(getCrunchifyURI(), crunchifyResourceConfig); } private static URI getCrunchifyURI() { return UriBuilder.fromUri("http://" + crunchifyGetHostName() + "/").port(8085).build(); } private static String crunchifyGetHostName() { String hostName = "localhost"; try { hostName = InetAddress.getLocalHost().getCanonicalHostName(); } catch (UnknownHostException e) { e.printStackTrace(); } return hostName; } }
Step 5:
Create your REST API. Create java class CrunchifyAPI.java
under same package “com.crunchify.tutorial
”
package com.crunchify.tutorial; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("api") public class CrunchifyAPI { @GET @Produces(MediaType.TEXT_PLAIN) public String get() { return "\n This is Crunchify REST API via HTTPServer"; } }
Step 6:
And that’s it. Now right click on JerseyEmbeddedHTTPServerCrunchify.java
and “Run it as Java Application
“.
Starting Crunchify's Embedded Jersey HTTPServer... Jun 30, 2014 3:45:26 PM com.sun.jersey.api.core.PackagesResourceConfig init INFO: Scanning for root resource and provider classes in the packages: com.crunchify.tutorial Jun 30, 2014 3:45:26 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses INFO: Root resource classes found: class com.crunchify.tutorial.CrunchifyAPI Jun 30, 2014 3:45:26 PM com.sun.jersey.api.core.ScanningResourceConfig init INFO: No provider classes found. Jun 30, 2014 3:45:27 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate INFO: Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM' Jersey Application Server started with WADL available at http://localhost:8085/application.wadl Started Crunchify's Embedded Jersey HTTPServer Successfully !!!
Step 7:
Validate Result. Visit this URL: http://localhost:8085/api
and you should see result on browser.
Hope you enjoy this tutorial. Need to share any comment?
I’m here to listen. Thanks.