本文介绍了无法让Jetty扫描带注释的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有嵌入式Jetty服务器的应用程序,我这样启动它(放在main()中,用Eclipse启动):
Server server = new Server(port);
WebAppContext context = new WebAppContext();
context.setResourceBase("web/");
context.setDescriptor("web/WEB-INF/web.xml");
context.setConfigurations(new Configuration[]{
new AnnotationConfiguration(), new WebXmlConfiguration(),
new WebInfConfiguration(), new TagLibConfiguration(),
new PlusConfiguration(), new MetaInfConfiguration(),
new FragmentConfiguration(), new EnvConfiguration()});
context.setContextPath("/");
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();
server.join();
我的web.xml如下所示(目前为空,不确定是否可以完全删除):
<web-app
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns="http://java.sun/xml/ns/javaee"
xsi:schemaLocation="http://java.sun/xml/ns/javaee http://java.sun/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false"
version="3.0">
</web-app>
我有一个简单的类设置如下:
import java.io.IOException;
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(urlPatterns={"/test"})
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/html/index.html").forward(request,response);
}
}
当我在web.xml中使用传统的servlet映射时,我的应用程序工作得很好。但是当我删除web.xml映射并使用注释时,我只得到404s。它看起来根本不像是在扫描注释。控制台如下所示:
2012-08-01 17:40:37.021:INFO:oejs.Server:jetty-8.1.5.v20120716
2012-08-01 17:40:37.227:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2012-08-01 17:40:37.294:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/me/project/web/}
2012-08-01 17:40:37.547:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/me/project/web/}
2012-08-01 17:40:37.547:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/me/project/web/}
2012-08-01 17:40:37.547:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/me/project/web/}
2012-08-01 17:40:37.641:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
我已经从我的研究中检查了一些东西:
Servlet-3.0 JAR在类路径中 web.xml中元数据完成设置为false 我已确保在Web应用上下文中包含AnnotationConfiguration我的想法已经用完,即将恢复到旧的web.xml,但为什么我无法让它工作,这让我苦不堪言。
推荐答案
更新:2021年6月
示例项目已更新为Jetty版本中立。
https://github/jetty-project/embedded-servlet-server
现在它有特定版本的Jetty的分支。
旧的示例项目已存档。
更新:2015年6月
Jetty 9和Servlet 3.1的示例项目已更新
参见:https://github/jetty-project/embedded-servlet-3.1
原始答案:
从您的描述和我使用您的代码创建的示例项目来看,您做的每件事都是正确的。
示例项目:https://github/jetty-project/embedded-servlet-3.0
为使此功能正常工作,您需要以下内容(仅提及这一点,因为您的问题不包括此详细信息)
JDK 1.6+ Jetty 8.1.x(或更高版本)中的jetty-webapps jar(+依赖项) Jetty 8.1.x(或更高版本)中的jetty-注解JAR(+依赖项)仅根据这些有限的要求,您就会看到以下依赖项列表。
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample-webapp 1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ sample-webapp ---
[INFO] company.sample:sample-webapp:war:1-SNAPSHOT
[INFO] +- org.eclipse.jetty.orbit:javax.servlet:jar:3.0.0.v201112011016:provided
[INFO] +- org.eclipse.jetty:jetty-webapp:jar:8.1.5-SNAPSHOT:test
[INFO] | +- org.eclipse.jetty:jetty-xml:jar:8.1.5-SNAPSHOT:test
[INFO] | | - org.eclipse.jetty:jetty-util:jar:8.1.5-SNAPSHOT:test
[INFO] | - org.eclipse.jetty:jetty-servlet:jar:8.1.5-SNAPSHOT:test
[INFO] | - org.eclipse.jetty:jetty-security:jar:8.1.5-SNAPSHOT:test
[INFO] | - org.eclipse.jetty:jetty-server:jar:8.1.5-SNAPSHOT:test
[INFO] | +- org.eclipse.jetty:jetty-continuation:jar:8.1.5-SNAPSHOT:test
[INFO] | - org.eclipse.jetty:jetty-http:jar:8.1.5-SNAPSHOT:test
[INFO] | - org.eclipse.jetty:jetty-io:jar:8.1.5-SNAPSHOT:test
[INFO] - org.eclipse.jetty:jetty-annotations:jar:8.1.5-SNAPSHOT:test
[INFO] +- org.eclipse.jetty:jetty-plus:jar:8.1.5-SNAPSHOT:test
[INFO] | +- org.eclipse.jetty.orbit:javax.transaction:jar:1.1.1.v201105210645:test
[INFO] | - org.eclipse.jetty:jetty-jndi:jar:8.1.5-SNAPSHOT:test
[INFO] | - org.eclipse.jetty.orbit:javax.mail.glassfish:jar:1.4.1.v201005082020:test
[INFO] | - org.eclipse.jetty.orbit:javax.activation:jar:1.1.0.v201105071233:test
[INFO] +- org.eclipse.jetty.orbit:javax.annotation:jar:1.1.0.v201108011116:test
[INFO] - org.eclipse.jetty.orbit:org.objectweb.asm:jar:3.1.0.v200803061910:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.771s
[INFO] Finished at: Fri Aug 10 18:17:46 MST 2012
[INFO] Final Memory: 6M/180M
[INFO] ------------------------------------------------------------------------
很可能您只是缺少依赖项或JDK要求。
这篇关于无法让Jetty扫描带注释的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论