6

私の単体テストでは、Jettyに基づく単純なテストサーバーを使用します。

package eu.kostia.textanalysis.webservices.jetty;

import java.awt.Desktop;
import java.net.URI;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class TestServer {
 static private final String CONTEXT_PATH = "/webservice";
 static private final String PROJECT_HOME = System.getenv("MY_WORKSPACE_HOME") + "/WebServices";
 static public final int PORT = 8080;

 private Server server;
 private Exception startException;

 private static class SingletonHolder {
  private static final TestServer INSTANCE = new TestServer();
 }

 /**
  * Returns the singleton instance of the test server.
  * 
  * @return the singleton instance of the test server.
  */
 public static TestServer getInstance() {
  return SingletonHolder.INSTANCE;
 }

 private TestServer() {
  server = new Server(PORT);

  WebAppContext context = new WebAppContext();

  context.setDescriptor(PROJECT_HOME + "/web/WEB-INF/web.xml");
  context.setResourceBase(PROJECT_HOME + "/web");
  context.setContextPath(CONTEXT_PATH);
  context.setParentLoaderPriority(true);


  server.setHandler(context); 
 }

 /**
  * Start the test server. This method returns only when the server is
  * complete started. There is no effect when you invoke this method and the
  * server is already running.
  */
 public void start() {  
  if (!server.isRunning()) {
   startException = null;
   new Thread("TestServer") {
    public void run() {
     try {
      server.start();
      server.join();
     } catch (Exception exc) {
      startException = exc;
     }
    }
   }.start();

   while (true) {
    if (startException != null) {
     throw new Error(startException);
    }

    // Block this method call until the server is started
    if (server.isStarted()) {
     return;
    }
   }
  }
 }

 /**
  * Stop the test server.
  */
 public void stop() {
  try {
   if (server.isRunning()) {
    server.stop();
   }
  } catch (Exception e) {
   throw new Error(e);
  }
 }

 /**
  * Returns {@code true} is the server is running.
  * 
  * @return {@code true} is the server is running.
  */
 public boolean isRunning() {
  return server.isRunning();
 }

 public static void main(String[] args) throws Exception {
  TestServer.getInstance().start();  
  Desktop.getDesktop().browse(new URI("http://localhost:8080/webservice/"));
 }

}

これは、web.xmlで構成されたサーブレットに対して非常にうまく機能しますが、サーブレット仕様3.0によって導入された新しい注釈構文を使用したいと思います。次に例を示します。

@WebServlet(urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {
         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                PrintWriter writer = response.getWriter();
                writer.print("<h1>HttpServlet using Servlet 3.0</h1>");
        }
}

TestServerクラスでJettyを構成して、アノテーションベースのサーブレットも処理するようにするにはどうすればよいですか?

4

3 に答える 3

1

さらに1年後に答えます。

現在のバージョンの Jetty (8.1) では、コマンド ラインで目的を正確に達成できます。

java -jar start.jar OPTIONS=annotations,plus etc/jetty-plus.xml

jetty ホーム ディレクトリから呼び出されます。

于 2012-12-12T18:40:50.783 に答える
0

Jetty 8はサーブレット3.0仕様を実装していますが、まだ実験段階です。

埋め込まれたglassfish3プラグインを使用してテストを実行することもできます。いくつかの情報については、以下のリンクを参照してください: http : //wikis.sun.com/display/GlassFish/3.1EmbeddedOnePager http://ocpsoft.com/java/using-embedded-glassfish-with-maven/ http:// embedded- glassfish.java.net/

これを書いていると、Jettyがよく使用される方法でGlassfishプラグインを使用するための信頼できるリソースがないことに気付きます。ただし、同様に機能します。

これが少なくとも少し役立つことを願っています。

于 2011-03-01T11:34:02.020 に答える