Created
July 6, 2019 13:42
-
-
Save Leejjon/746f5495a1356843cf7b42c8df799497 to your computer and use it in GitHub Desktop.
Main.java with hostname and port as arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.glassfish.grizzly.http.server.HttpServer; | |
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import java.net.URI; | |
/** | |
* Main class. | |
* | |
*/ | |
public class Main { | |
// Base URI the Grizzly HTTP server will listen on | |
private static final String BASE_URI = "http://%1$s:%2$s/"; | |
/** | |
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application. | |
* @return Grizzly HTTP server. | |
*/ | |
static HttpServer startServer(String base, String port) { | |
// create a resource config that registers the MyResource JAX-RS resource | |
final ResourceConfig rc = new ResourceConfig(); | |
// Registering like this will give warnings like: | |
// WARNING: A provider com.example.MyResource registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider | |
// com.example.MyResource will be ignored. | |
// But it just works and according to stackoverflow this is a bug: | |
// https://github.com/jersey/jersey/issues/3700 | |
rc.register(new MyResource()); | |
// Disable wadl because I never asked for this. | |
rc.property("jersey.config.server.wadl.disableWadl", true); | |
// create and start a new instance of grizzly http server | |
// exposing the Jersey application at BASE_URI | |
return GrizzlyHttpServerFactory.createHttpServer(URI.create(String.format(BASE_URI, base, port)), rc); | |
} | |
/** | |
* Main method. | |
* @param args Should contain "localhost" or "0.0.0.0". | |
*/ | |
public static void main(String[] args) { | |
String base = args[0]; | |
String port = args[1]; | |
startServer(base, port); | |
System.out.println(String.format("Jersey app started at %s", String.format(BASE_URI, base, port))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment