1

我有一个在单元测试中使用的嵌入式 servlet,如下所示:

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }

    public void handle(String url, HttpServletRequest request, HttpServletResponse response, int arg3) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        if(content != null) {
            final ServletOutputStream stream = response.getOutputStream();

            stream.print(content);
            stream.flush();
            stream.close();
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    ....
}

这是使用 Jetty 6.1.24 版本编写的。我尝试切换到使用 Jetty 7.1.1.v20100517,并将该代码更新为:

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }


    public void handle(String url, Request request, HttpServletRequest servletRequest, HttpServletResponse response) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        request.setHandled(true);

        response.setContentType("application/json");

        if(content != null) {
            response.setStatus(HttpServletResponse.SC_OK);
            final Writer stream = response.getWriter();

            stream.append(content);
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}

但是每当我尝试向服务器发出请求时,它都会无限期地挂起。有没有人经历过类似的事情?它还将它打印到日志中:

log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
org.eclipse.jetty.server.Server@670655dd STOPPED
 +-UnitTestWebservices@50ef5502 started
4

1 回答 1

0

似乎该特定版本可能存在问题。我使用此代码更改为版本 7.0.2.v20100331

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }

    public void handle(String url, final Request rawRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        if(content != null) {
            final ServletOutputStream stream = response.getOutputStream();

            stream.print(content);
            stream.flush();
            stream.close();
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}

现在一切似乎都运行良好。

于 2010-05-27T08:53:24.753 回答