首页 > 代码库 > 嵌入式jetty9启动标准webapp目录
嵌入式jetty9启动标准webapp目录
package com.doctor.embeddedjetty; import java.net.URISyntaxException; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; /** * 标准spring 配置(java config) 嵌入式jetty9启动,支持jsp试图,标准webapp目录,测试用例看 * {@link EmbeddedJettyServer4ForWebappTest} EmbeddedJettyServer4ForWebappTest * @author doctor * * @since 2015年1月6日 下午10:40:16 */ public class EmbeddedJettyServer4ForWebapp { private int port; private String resourceBase; private Server server; public EmbeddedJettyServer4ForWebapp(String resourceBase) { this(8080, resourceBase); } public EmbeddedJettyServer4ForWebapp(int port,String resourceBase) { this.resourceBase = resourceBase; this.port = port; init(); } /** * * @param port * @param resourceBase 注:这里是相对路径,web src/test/resources路径,绝对路径没判断 * @param springRootConfiguration * @param springMvcConfiguration */ public void init() { server = new Server(port); WebAppContext context = new WebAppContext(); context.setContextPath("/"); try { context.setResourceBase(this.getClass().getResource(resourceBase).toURI().toASCIIString()); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } server.setHandler(context); } public void start() throws Exception { if (server != null) { if (server.isStarting() || server.isStarted() || server.isRunning()) { return; } } TimeUnit.SECONDS.sleep(3); server.start(); } public void stop() throws Exception { if (server != null) { if (server.isRunning()) { server.stop(); } } } public void join() throws InterruptedException { if (server != null) { server.join(); } } }
测试用例
<pre name="code" class="java">package com.doctor.embeddedjetty; import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Response; import org.junit.Test; public class EmbeddedJettyServer4ForWebappTest { @Test public void test() throws Throwable{ EmbeddedJettyServer4ForWebapp server = new EmbeddedJettyServer4ForWebapp(8789,"/embeddedJettyServer4ForWebapp"); server.start(); Response response = Request.Get("http://localhost:8789/jetty/test.html").execute(); System.out.println(response.returnContent().asString()); response = Request.Get("http://localhost:8789/jetty/test.json").execute(); System.out.println(response.returnContent().asString()); server.stop(); } }
输出:
01-10 10:23:36.627 main INFO o.e.j.u.log - Logging initialized @503ms 01-10 10:23:39.728 main INFO o.e.j.s.Server - jetty-9.3.0.M1 01-10 10:23:39.850 main INFO / - Initializing Spring root WebApplicationContext 01-10 10:23:39.850 main INFO o.s.w.c.ContextLoader - Root WebApplicationContext: initialization started 01-10 10:23:39.939 main INFO o.s.w.c.s.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sat Jan 10 10:23:39 CST 2015]; root of context hierarchy 01-10 10:23:39.995 main INFO o.s.b.f.x.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [embeddedJettyServer4ForWebapp/config/spring-context.xml] 01-10 10:23:40.099 main INFO o.s.w.c.ContextLoader - Root WebApplicationContext: initialization completed in 249 ms 01-10 10:23:40.211 main INFO / - Initializing Spring FrameworkServlet 'dispatcherServlet' 01-10 10:23:40.211 main INFO o.s.w.s.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started 01-10 10:23:40.214 main INFO o.s.w.c.s.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Sat Jan 10 10:23:40 CST 2015]; parent: Root WebApplicationContext 01-10 10:23:40.215 main INFO o.s.b.f.x.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [embeddedJettyServer4ForWebapp/config/spring-controller.xml] 01-10 10:23:40.718 main INFO o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/jetty/test.html || /jetty/test.json],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.doctor.embeddedjetty.EmbeddedJettyServerController.test2() 01-10 10:23:40.721 main INFO o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.doctor.embeddedjetty.SimpleController.hello() 01-10 10:23:40.722 main INFO o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/embeddedJettyServer2Test],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.doctor.embeddedjetty.SImpleController2.getMessage() 01-10 10:23:40.841 main INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Sat Jan 10 10:23:40 CST 2015]; parent: Root WebApplicationContext 01-10 10:23:40.882 main INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Sat Jan 10 10:23:40 CST 2015]; parent: Root WebApplicationContext 01-10 10:23:40.935 main INFO o.s.w.s.h.SimpleUrlHandlerMapping - Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController] 01-10 10:23:41.020 main INFO o.s.w.s.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 808 ms 01-10 10:23:41.020 main INFO o.e.j.s.h.ContextHandler - Started o.e.j.w.WebAppContext@63787180{/,file:///home/doctor/workspace-sts-3.6.2.RELEASE/doctor/springmvc-practice/target/test-classes/embeddedJettyServer4ForWebapp/,AVAILABLE} 01-10 10:23:41.026 main INFO o.e.j.s.ServerConnector - Started ServerConnector@24111ef1{HTTP/1.1,[http/1.1]}{0.0.0.0:8789} 01-10 10:23:41.027 main INFO o.e.j.s.Server - Started @4905ms <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> hello jetty test for webapp /jettytest/test/jsp </body> </html> {"hashMap":{"test":"json","test-html":"html","what":"what"}} 01-10 10:23:42.001 main INFO o.e.j.s.ServerConnector - Stopped ServerConnector@24111ef1{HTTP/1.1,[http/1.1]}{0.0.0.0:8789} 01-10 10:23:42.002 main INFO / - Destroying Spring FrameworkServlet 'dispatcherServlet' 01-10 10:23:42.002 main INFO o.s.w.c.s.XmlWebApplicationContext - Closing WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Sat Jan 10 10:23:40 CST 2015]; parent: Root WebApplicationContext 01-10 10:23:42.012 main INFO / - Closing Spring root WebApplicationContext 01-10 10:23:42.013 main INFO o.s.w.c.s.XmlWebApplicationContext - Closing Root WebApplicationContext: startup date [Sat Jan 10 10:23:39 CST 2015]; root of context hierarchy 01-10 10:23:42.016 main INFO o.e.j.s.h.ContextHandler - Stopped o.e.j.w.WebAppContext@63787180{/,file:///home/doctor/workspace-sts-3.6.2.RELEASE/doctor/springmvc-practice/target/test-classes/embeddedJettyServer4ForWebapp/,UNAVAILABLE}
嵌入式jetty9启动标准webapp目录
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。