在编程配置的Jetty嵌入式服务器中使用HttpRequestHandlerServlet

时间:2013-12-28 01:30:34

标签: java spring jetty spring-remoting httpinvoker

我以编程方式定义了一个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.web.context.support.HttpRequestHandlerServlet-406774688'的bean     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)     在org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)     在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)     在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)     在org.springframework.web.context.support.HttpRequestHandlerServlet.init(HttpRequestHandlerServlet.java:58)     在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。

非常感谢任何帮助。

Steve Ardis

1 个答案:

答案 0 :(得分:1)

我知道“目标bean名称必须与web.xml中定义的HttpRequestHandlerServlet servlet-name相匹配”(请参阅​​HttpRequestHandlerServlet上的JavaDoc),但我只使用了编程方法所需的一半。 / p>

添加以下代码让我解决了这个问题:

servletHolder.setName("testServlet");

Steve Ardis