I have programatically defined a Jetty server and added an instance of a HttpRequestHandlerServlet. I am trying to do all of this without a web.xml file. Here is a simplified version of my code:
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());
}
}
If you run the test, the following exception (root cause) occurs:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.web.context.support.HttpRequestHandlerServlet-406774688' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:270) at 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) at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:477)
I am using Jetty 7.6.9.v20130131 (due to a dependency in my real project on Camel 2.12.1) and Spring 3.2.4.RELEASE.
Any help is greatly appreciated.
Steve Ardis
I knew that "The target bean name must match the HttpRequestHandlerServlet servlet-name as defined in web.xml" (see the JavaDoc on HttpRequestHandlerServlet), but I was only doing half of what was required when using the programmatic approach.
Adding the following code got me past this issue:
servletHolder.setName("testServlet");
Steve Ardis