मेरी यूनिट-परीक्षण के लिए मैं एक साधारण परीक्षण सर्वर जेट्टी के आधार पर का उपयोग करें: सर्वलेट के लिए बहुत अच्छी तरह सेसर्वलेट एम्बेडेड जेट्टी में 3.0 समर्थन 8.0
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 वर्ग भी एनोटेशन आधारित सर्वलेट को संसाधित करने के घाट कॉन्फ़िगर shoud?
मुझे एहसास है कि पोस्ट प्राचीन है, लेकिन मुझे पता चला कि इस पोस्ट के बाद भी मुझे एम्बेडेड सर्वर के साथ एक ही समस्या थी। –
मैंने पाया कि AsyncContext जेटी में बिल्कुल सही काम नहीं कर रहा था। मैंने इसे एंबेडेड टॉमकैट 7 के पक्ष में रहने के लिए छोड़ दिया। जो कि समय के लिए अच्छी तरह से काम कर रहा है। –