Embedded Jetty找不到Annotated Servlet

kDo*_*Dot 3 java annotations servlets jetty embedded-jetty

简短:我有一个提供war工件的项目,其中包含带注释但没有web.xml的servlet.如果我尝试在jetty中使用war,我总是只获得war内容的目录列表,而不是servlet执行.

任何的想法?

长话:我的servlet看起来像这样

package swa;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet( asyncSupported = false, urlPatterns={"/*"})
public class ServletX extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html");

        // Actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println("<h1>Hi there..</h1>");
    }

}
Run Code Online (Sandbox Code Playgroud)

所以我猜没什么特别的.当我使用mvn jetty:run一切都很好.确保这一点后,项目被打包成一个战争档案.

此war存档在另一个必须在代码中设置jetty的项目中使用.这就是它的完成方式:

        String jettyPort = properties.getProperty("jetty.port", "8080");
        Server server = new Server();

        ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory());
        httpConnector.setPort(Integer.parseInt(jettyPort));
        httpConnector.setAcceptQueueSize(2);
        httpConnector.setHost("0.0.0.0");
        httpConnector.setIdleTimeout(30000);
        server.setConnectors(new Connector[] { httpConnector });

        WebAppContext wacHandler = new WebAppContext();
        wacHandler.setContextPath("/admin");
        wacHandler.setWar("swa-0.0.1-SNAPSHOT.war");
        wacHandler.setConfigurationDiscovered(true);

        server.setHandler(wacHandler);

        server.start();
Run Code Online (Sandbox Code Playgroud)

执行此项目时,日志告诉我发现了战争.但是,如果我打开网址,http://localhost:8080/admin我只会看到战争内容的列表(而不是"你好").

有人能指出我的失败吗?

Joa*_*elt 6

您需要适当地(并以正确的顺序)定义WebAppContext配置.

    wacHandler.setConfigurations(new Configuration[]
    { 
        new AnnotationConfiguration(), 
        new WebInfConfiguration(), 
        new WebXmlConfiguration(), 
        new MetaInfConfiguration(), 
        new FragmentConfiguration(),
        new EnvConfiguration(), 
        new PlusConfiguration(), 
        new JettyWebXmlConfiguration() 
    });
Run Code Online (Sandbox Code Playgroud)

别忘了添加jetty-annotations.jar.

这是来自Servlet 3.1的使用EmbedMe.java示例

https://github.com/jetty-project/embedded-servlet-3.1/


Mar*_*sny 6

除了添加上述答案中所述的必要配置外,还需要强制 Jetty 扫描当前项目编译的类,默认情况下 Jetty 会忽略这些类。为此,只需在您的 WebAppContext 上调用以下内容:

context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/classes/.*")
Run Code Online (Sandbox Code Playgroud)

请在此处查看完整示例(在 Kotlin 中):https : //github.com/mvysny/vaadin-on-kotlin/blob/master/vok-example-crud/src/test/java/com/github/vok/example /crud/Server.kt

这非常有效,并发现所有 @WebServlets、@WebListeners 和其他。