嵌入式 Jetty 找不到带注释的 Servlet [英] Embedded Jetty does not find Annotated Servlet

查看:60
本文介绍了嵌入式 Jetty 找不到带注释的 Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Short: I have a project that provides a war artifact which includes a servlet with annotations but no web.xml. If i try to use the war in jetty i always get only the directory listing of the war content but not the servlet execution.

有什么想法吗?

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

Long story: My servlets look like this

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>");
    }

}

所以我想没什么特别的.当我使用 mvn jetty:run 时一切正常.在确保这一点后,项目被打包到一个战争档案中.

So nothing special i guess. When i use mvn jetty:run everything is fine. After ensuring this the project is packed into a war-archive.

这个战争档案在另一个必须在代码中设置码头的项目中使用.它是这样完成的:

This war archive is used within another project that has to set up jetty within code. This is how its done:

        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();

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

When executing this project the logs tell me that the war is found. But if i open the url http://localhost:8080/admin i only see the listing of the war content (instead of 'Hi there').

有人能指出我的失败吗?

Can someone point me to my failure?

推荐答案

更新 - 2021 年 8 月

从 Jetty 10.0.0(包括 Jetty 11.0.0)开始,此过程发生了变化

Updated - Aug 2021

This process has changed starting in Jetty 10.0.0 (this includes Jetty 11.0.0)

jetty-annotations-.jar 在类路径上的存在足以为您的 servlet 和 websocket 层启用注释和字节码扫描.

The existence of the jetty-annotations-<ver>.jar on the classpath is enough to enable the annotation and bytecode scanning for your servlets and websocket layers.

不应再使用 WebAppContext.setConfiguration(...) 方法.

旧的示例项目已存档并替换为

The old example project has been archived and replaced with

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

有...的例子

<头>
Servlet API 版本码头版新分支
3.1码头 9.4.xembedded-servlet-server : jetty-9.4.x
4.0码头 10.xembedded-servlet-server : jetty-10.0.x
5.0码头 11.xembedded-servlet-server : jetty-11.0.x

原始答案 - 2014 年 9 月

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

Original Answer - Sept 2014

You need to define the WebAppContext configurations appropriately (and in the correct order).

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

不要忘记添加jetty-annotations.jar.

这是来自 EmbedMe.java 示例 与 Servlet 3.1 一起使用的问题

This is from the EmbedMe.java example for embedded-jetty use with Servlet 3.1 found at

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

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

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