Created
January 11, 2017 07:08
-
-
Save digvijaybhakuni/dcefe5db113dba29a1022b1000716354 to your computer and use it in GitHub Desktop.
Building Server with Undertow
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
import static io.undertow.servlet.Servlets.servlet; | |
import javax.servlet.ServletException; | |
import org.glassfish.jersey.servlet.ServletContainer; | |
import io.undertow.Handlers; | |
import io.undertow.Undertow; | |
import io.undertow.server.handlers.PathHandler; | |
import io.undertow.servlet.Servlets; | |
import io.undertow.servlet.api.DeploymentInfo; | |
import io.undertow.servlet.api.DeploymentManager; | |
public class App { | |
private static Undertow server; | |
public static void main(String[] args) throws ServletException { | |
startContainer(9090); | |
} | |
public static void stopContainer() { | |
server.stop(); | |
} | |
public static void startContainer(int port) throws ServletException { | |
io.undertow.servlet.api.ServletContainer servletContainer = Servlets.defaultContainer(); | |
DeploymentInfo di = Servlets.deployment().setClassLoader(App.class.getClassLoader()).setContextPath("/jaxrx") | |
.setDeploymentName("jaxrx-demo-1.0") | |
.addServlets(servlet("jerseyServlet", ServletContainer.class).setLoadOnStartup(1) | |
.setAsyncSupported(true).addInitParam("javax.ws.rs.Application", JerseyApp.class.getName()) | |
.addMapping("/api/*")) | |
.addWelcomePage("index.html"); | |
DeploymentManager manager = servletContainer.addDeployment(di); | |
manager.deploy(); | |
PathHandler path = Handlers.path(Handlers.redirect("/jaxrx")).addPrefixPath("/jaxrx", manager.start()); | |
server = Undertow.builder().addHttpListener(port, "localhost").setHandler(path).build(); | |
server.start(); | |
} | |
} | |
private static Undertow server; | |
public static void main(String[] args) throws ServletException { | |
startContainer(9090); | |
} | |
public static void stopContainer() { | |
server.stop(); | |
} | |
public static void startContainer(int port) throws ServletException { | |
io.undertow.servlet.api.ServletContainer servletContainer = Servlets.defaultContainer(); | |
DeploymentInfo di = Servlets.deployment().setClassLoader(App.class.getClassLoader()).setContextPath("/jaxrx") | |
.setDeploymentName("jaxrx-demo-1.0") | |
.addServlets(servlet("jerseyServlet", ServletContainer.class).setLoadOnStartup(1) | |
.setAsyncSupported(true).addInitParam("javax.ws.rs.Application", JerseyApp.class.getName()) | |
.addMapping("/api/*")) | |
.addWelcomePage("index.html"); | |
DeploymentManager manager = servletContainer.addDeployment(di); | |
manager.deploy(); | |
PathHandler path = Handlers.path(Handlers.redirect("/jaxrx")).addPrefixPath("/jaxrx", manager.start()); | |
server = Undertow.builder().addHttpListener(port, "localhost").setHandler(path).build(); | |
server.start(); | |
} | |
} |
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
group 'com.dgstack.eg.jaxrs' | |
version '1.0' | |
apply plugin: 'java' | |
apply plugin: 'war' | |
apply plugin: 'application' | |
sourceCompatibility = 1.8 | |
mainClassName = 'com.dgstack.eg.jaxrx.App' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
//Jersey servlet dependencies | |
compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.25' | |
//Undertow dependencies | |
compile group: 'io.undertow', name: 'undertow-core', version: '1.4.7.Final' | |
compile group: 'io.undertow', name: 'undertow-servlet', version: '1.4.7.Final' | |
//J2ee servlet dependencies | |
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0' | |
} | |
task runApp(type: JavaExec) { | |
classpath = sourceSets.main.runtimeClasspath | |
main = 'com.dgstack.eg.jaxrx.App' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment