嵌入式Jetty 8.0中的Servlet 3.0支持 [英] Servlet 3.0 support in embedded Jetty 8.0

查看:151
本文介绍了嵌入式Jetty 8.0中的Servlet 3.0支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的单元测试,我使用一个基于Jetty的简单测试服务器:

For my unit-tests I use a simple test-server based on 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/"));
 }

}

对于已配置的servlet非常有效在web.xml中,但是我现在想使用Servlet Specification 3.0引入的新注释语法,例如:

It works very well for servlet configured in web.xml but I'd now like to use the new annotation syntax introduced by the Servlet Specification 3.0, for example:

@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进行处理还是基于注释的servlet?

How shoud I configure Jetty in my TestServer class to process also annotation-based servlets?

推荐答案

添加到您的代码中

context.setConfigurations(new Configuration[] {
                new AnnotationConfiguration(), new WebXmlConfiguration(),
                new WebInfConfiguration(), new TagLibConfiguration(),
                new PlusConfiguration(), new MetaInfConfiguration(),
                new FragmentConfiguration(), new EnvConfiguration() });

您只需要设置AnnotationConfiguration即可自动发现带注释的类。其余配置都是如此,因此您可以启用容器的其他方面。假设您应该可以使用OPTIONS = annotations,jsp,(etc ...)从命令行执行此操作,但是我从未成功过。至少通过这种方式,它应该可以在嵌入式环境中正确地选择带注释的类。

You only need to set the AnnotationConfiguration to get the auto-discovery of annotated classes to work. The rest of the configurations are so you can enable other aspects of the container. Supposedly you should be able to do this from the commandline, using OPTIONS=annotations,jsp,(etc...), but I never got that working. At least this way it should pick up your annotated classes properly in the embedded environment.

此外,Eclipse码头项目默认情况下处于关闭状态,而riptide声称默认将其打开。我猜这是配置文件中的区别。

Also as a side note it appears the Eclipse jetty project has annotation turned off by default, whereas riptide claims to have them turned on by default. I'm guessing this is a difference in the configuration files.

这篇关于嵌入式Jetty 8.0中的Servlet 3.0支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆