Tag:jetty embedded jetty
Package Com.doctor.embeddedjetty;import Java.util.concurrent.timeunit;import Org.eclipse.jetty.server.Server; Import Org.eclipse.jetty.servlet.servletcontexthandler;import Org.eclipse.jetty.servlet.servletholder;import Org.springframework.web.context.support.annotationconfigwebapplicationcontext;import Org.springframework.web.servlet.dispatcherservlet;public class Embeddedjettyserver {private int port;p Rivate class <?> springconfiguration = null;private Server server;public embeddedjettyserver (class<?> Springconfiguration) {This (8080, springconfiguration);} Public embeddedjettyserver (int port,class<?> springconfiguration) {this.port = Port;this.springconfiguration = Springconfiguration;init ();} public void init () {server = new server (port); Servletcontexthandler context = new Servletcontexthandler (); Context.setcontextpath ("/"); Server.sethandler (context) ; Annotationconfigwebapplicationcontext webapplicationcontext = new Annotationconfigwebapplicationcontext (); WebapplicationconText.register (springconfiguration);D ispatcherservlet dispatcherservlet = new Dispatcherservlet ( Webapplicationcontext); Context.addservlet (new Servletholder (Dispatcherservlet), "/*");} 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 ();}}}
Test code:
Package Com.doctor.embeddedjetty;import static Org.junit.assert.*;import static Org.hamcrest.core.isequal.*;import Org.apache.http.client.fluent.request;import Org.apache.http.client.fluent.response;import Org.junit.Test;import Org.springframework.context.annotation.componentscan;import Org.springframework.context.annotation.configuration;public class Embeddedjettyforspringmvctest {<span style= " White-space:pre "></span><span style=" White-space:pre "></span> @Test <span style=" White-space:pre "></span>public void Test () throws Throwable{<span style=" White-space:pre "></span >embeddedjettyserver jettyserver = new Embeddedjettyserver (springconfiguration.class); <span style= " White-space:pre "></span>jettyserver.start (); <span style=" White-space:pre "></span><span Style= "White-space:pre" ></span>response Response = Request.get ("http://localhost:8080/"). Execute ();< Span style= "White-space:pre" ></span>assertthaT (Response.returncontent (). asstring (), Equalto ("Hello")) <span style= "White-space:pre" ></span> Jettyserver.stop (); <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></ Span><span style= "White-space:pre" ></span><span style= "White-space:pre" ></span>@ Configuration<span style= "White-space:pre" ></span> @ComponentScan ("Com.doctor.embeddedjetty") < Span style= "White-space:pre" ></span>static class Springconfiguration{<span style= "White-space:pre" > </span><span style= "White-space:pre" ></SPAN>}
Embedded jetty Start Spring (Java configuration method), JUnit test