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

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

问题描述

简短: 我有一个项目,提供了一个战争工件,其中包括带有注解但没有web.xml的servlet.如果我尝试在码头上使用战争,我总是只会获得战争内容的目录列表,而不会获得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();

执行该项目时,日志告诉我已找到战争.但是,如果我打开网址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?

推荐答案

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

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 示例" class ="post-tag" title =显示标记为'embedded-jetty'的问题rel ="tag">嵌入式码头与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天全站免登陆