我以编程方式定义了一个 Jetty 服务器并添加了一个 HttpRequestHandlerServlet 的实例。我正在尝试在没有 web.xml 文件的情况下完成所有这些工作。这是我的代码的简化版本:
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Test;
import static org.mockito.Mockito.*;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
public class TestServer extends Server {
public TestServer() throws Exception {
super(8888);
final ServletContextHandler contextHandler = new ServletContextHandler(this, "/", ServletContextHandler.NO_SESSIONS|ServletContextHandler.NO_SECURITY);
final ServletContext context = contextHandler.getServletContext();
StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("testServlet", TestBean.class);
applicationContext.refresh();
GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
webApplicationContext.setParent(applicationContext);
webApplicationContext.setServletContext(context);
webApplicationContext.refresh();
context.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
ServletConfig servletConfig = new ServletConfig() {
@Override
public String getServletName() {
return "testServlet";
}
@Override
public ServletContext getServletContext() {
return context;
}
@Override
public String getInitParameter(String name) {
return null;
}
@Override
public Enumeration<String> getInitParameterNames() {
return null;
}
};
HttpRequestHandlerServlet httpRequestHandlerServlet = new HttpRequestHandlerServlet();
httpRequestHandlerServlet.init(servletConfig);
ServletHolder servletHolder = new ServletHolder(httpRequestHandlerServlet);
contextHandler.addServlet(servletHolder, "/testBean");
start();
}
@Test
public void test() throws Exception {
TestServer testServer = new TestServer();
Request baseRequest = new Request();
HttpServletRequest request = mock(HttpServletRequestWrapper.class);
HttpServletResponse response = mock(HttpServletResponseWrapper.class);
testServer.handle("/testBean", baseRequest, request, response);
}
}
class TestBean implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().write("Steve".getBytes());
}
}
如果您运行测试,则会发生以下异常(根本原因):
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory. java:570) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108) 在 org.springframework .beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:270) 在 org.springframework.beans.factory.support.AbstractBeanFactory .getBean(AbstractBeanFactory.java:198) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121) at org.springframework.web.context.support.HttpRequestHandlerServlet.init(HttpRequestHandlerServlet.java:58) at javax .servlet.GenericServlet.init(GenericServlet.java:241) 在 org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:477)
我正在使用 Jetty 7.6.9.v20130131(由于我的真实项目中依赖于 Camel 2.12.1)和 Spring 3.2.4.RELEASE。
任何帮助是极大的赞赏。
史蒂夫·阿迪斯