undertow multiple webservices url

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I want something like this on my address. How I can bind servlet1, servlet2, servlet3 to localhost:8080 so I can have different url to call such as

localhost:8080/servlet

localhost:8080/servlet1

localhost:8080/servlet2

localhost:8080/servlet3

I have this code that only work for thw last url(servlet) added

DeploymentInfo servletBuilder = deployment()                 .setClassLoader(ServletServer.class.getClassLoader())                 .setContextPath(MYAPP)                 .setDeploymentName("test.war")                 .addServlets(                         servlet(servletName,  x)                         .addInitParam("message", output)                         .addMapping("/"+servletName.toLowerCase()));          DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);         manager.deploy();          HttpHandler servletHandler = manager.start();         path = Handlers.path(Handlers.redirect(MYAPP))                 .addPrefixPath(MYAPP, servletHandler); 

回答1:

DeploymentInfo servletBuilder = deployment() .addServlets(  servlet("servlet1", YourServlet1.class).addInitParam("param1", "Value A") .addMapping("/servlet1/*"),  servlet("servlet2", YourServlet2.class).addInitParam("param2", "Value B") .addMapping("/servlet2/*"),  servlet("servlet3", YourServlet3.class).addInitParam("param2", "Value C") .addMapping("/servlet3/*")  // as many as you like. ); 

Taken using info from here: https://github.com/undertow-io/undertow/tree/master/examples/src/main/java/io/undertow/examples/servlet



回答2:

Modified from Undertow 1.2.5 sample code:

Runnable Project from this URL, will expire on June 10, 2015: https://www.hightail.com/download/bXBaTXRRaFJQb0xIRHRVag


package servlet;  import javax.servlet.ServletException;  import io.undertow.Handlers; import io.undertow.Undertow;  import io.undertow.server.HttpHandler; import io.undertow.server.handlers.PathHandler; import io.undertow.servlet.api.DeploymentInfo; import io.undertow.servlet.api.DeploymentManager;  import static io.undertow.servlet.Servlets.defaultContainer; import static io.undertow.servlet.Servlets.deployment; import static io.undertow.servlet.Servlets.servlet;  /**  * @author Stuart Douglas  */ public class ServletServer {      public static final String MYAPP = "/myapp";      public static void main(final String[] args) {         try {              DeploymentInfo servletBuilder = deployment()                     .setClassLoader(ServletServer.class.getClassLoader())                     .setContextPath(MYAPP)                     .setDeploymentName("doesnotmatter.war")                     .addServlets(                             servlet("MessageServletA", MessageServletA.class)                             .addInitParam("message", "Hello AAA\n")                             .addMapping("/msgA/*"),                             servlet("MessageServletB", MessageServletB.class)                             .addInitParam("message", "Hello BBB\n")                             .addMapping("/msgB/*"),                             servlet("MessageServletC", MessageServletC.class)                             .addInitParam("message", "Hello CCC\n")                             .addMapping("/msgC/*")                                    );              DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);             manager.deploy();              HttpHandler servletHandler = manager.start();             PathHandler path = Handlers.path(Handlers.redirect(MYAPP))                     .addPrefixPath(MYAPP, servletHandler);             Undertow server = Undertow.builder()                     .addHttpListener(8080, "localhost")                     .setHandler(path)                     .build();             server.start();         } catch (ServletException e) {             throw new RuntimeException(e);         }     } } 

Please test with this command from shell:


# Runs in background java -cp target/undertowservlet-jar-with-dependencies.jar servlet.Servletrver &  curl http://localhost:8080/myapp/msgA/ curl http://localhost:8080/myapp/msgB/ curl http://localhost:8080/myapp/msgC/ 



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!