kos*_*tia 6 servlets embedded-jetty
对于我的单元测试,我使用一个基于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/"));
}
}
Run Code Online (Sandbox Code Playgroud)
它适用于在web.xml中配置的servlet,但我现在想使用Servlet Specification 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>");
}
}
Run Code Online (Sandbox Code Playgroud)
我是如何在我的TestServer类中配置Jetty以处理基于注释的servlet的?
添加到您的代码中
context.setConfigurations(new Configuration[] {
new AnnotationConfiguration(), new WebXmlConfiguration(),
new WebInfConfiguration(), new TagLibConfiguration(),
new PlusConfiguration(), new MetaInfConfiguration(),
new FragmentConfiguration(), new EnvConfiguration() });
Run Code Online (Sandbox Code Playgroud)
您只需设置AnnotationConfiguration即可自动发现带注释的类.其余配置是您可以启用容器的其他方面.据说你应该能够从命令行执行此操作,使用OPTIONS = annotations,jsp,(etc ...),但我从来没有这样做.至少这种方式应该在嵌入式环境中正确地获取带注释的类.
另外作为旁注,Eclipse jetty项目默认关闭注释,而riptide声称默认情况下启用它们.我猜这是配置文件的一个区别.
小智 0
Jetty 8 正在实现 servlet 3.0 规范,但它仍处于实验阶段。
您还可以使用嵌入式 glassfish 3 插件来运行测试。请参阅以下链接了解一些信息: http://wikis.sun.com/display/GlassFish/3.1EmbeddedOnePager http://ocpsoft.com/java/using-embedded-glassfish-with-maven/ http://embedded- glassfish.java.net/
当我写这篇文章时,我意识到没有权威资源可以像 Jetty 常用的方式使用 Glassfish 插件。但它确实以类似的方式工作。
我希望这至少有一点帮助。
| 归档时间: |
|
| 查看次数: |
8611 次 |
| 最近记录: |