How to Start Embedded HTTP Jersey server during Java Application Startup
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 addCross-origin resource sharing
to your Web Jersey Application. Please follow this tutorial for the same: http://crunfy.me/1DZIui5Well, 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:
Step 3:
In this project we just need to one jersey-server dependency.Step 4:
CreateJerseyEmbeddedHTTPServerCrunchify.java
under “com.crunchify.tutorial
” package.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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: http://crunfy.me/1DZIui5
//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 classCrunchifyAPI.java
under same package “com.crunchify.tutorial
”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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 onJerseyEmbeddedHTTPServerCrunchify.java
and “Run it as Java Application
“.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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.
Have anything to add to this article? Please chime in and join the conversion.
0 comments :