Rya*_*lva 18 annotations jetty embedded-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();
Run Code Online (Sandbox Code Playgroud)
我的web.xml看起来像这样(现在是空的,我不确定我是否可以完全删除它):
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false"
version="3.0">
</web-app>
Run Code Online (Sandbox Code Playgroud)
我有一个简单的类设置如下:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我在web.xml中使用传统的servlet映射时,我的应用程序工作正常.但是当我删除web.xml映射并使用注释时,我只能获得404.看起来它根本不是在扫描注释.控制台看起来像这样:
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
Run Code Online (Sandbox Code Playgroud)
我已经从我的研究中检查了一些事情:
我已经没有想法了,我只想恢复到旧的web.xml,但这让我无法解决为什么我无法让它工作.
更新:2015年6月
Jetty 9和Servlet 3.1的示例项目已更新
请参阅:https://github.com/jetty-project/embedded-servlet-3.1
原答案:
根据您的描述和我使用您的代码掀起的示例项目,您正在正确地完成所有工作.
示例项目:https://github.com/jetty-project/embedded-servlet-3.0
为了实现这一点,您需要以下内容(仅提及此问题,因为您的问题未包含此详细信息)
仅从这些有限的要求,您将看到以下依赖列表.
$ 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] com.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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
您很可能只是缺少依赖项或JDK要求.
小智 6
AnntationConfiguration类通过其scanForAnnotations(WebAppContext)方法扫描注释.在方法AnnotationConfiguration类中扫描以下路径.
因此,如果您希望src/main/java扫描生产代码中的servlet类(即目录中的源代码),请将生产代码添加到WebAppContext元数据中WEB-INF/classes.
尝试下面的代码,将代码添加到WebAppContext元数据中.
URL classes = getClass()
.getProtectionDomain()
.getCodeSource()
.getLocation();
WebAppContext context = new WebAppContext();
context.getMetaData()
.setWebInfClassesDirs(
Arrays.asList(Resource.newResource(classes)));
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,但经过多次阅读后,我得到了解决方案!重要提示:请记住在您的构建路径中包含以下 jars:
jetty-all-9.0.6.v20130930.jar
jetty-annotations-9.0.6.v20130930.jar
org.objectweb.asm-3.1.0.v200803061910.jar
javax.servlet-api-3.0.1.jar
jetty-plus-9.0.6.v20130930.jar
public class Main {
public static void main(String[] args) throws Exception {
//Create the server
Server server = new Server(8080);
ClassList clist = ClassList.setServerDefault(server);
//clist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
clist.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName());
//Here is the trick to scan current classpath!
webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
".*/build/classes/");
webapp.setContextPath("/");
webapp.setResourceBase("./WebContent");
server.setHandler(webapp);
server.start();
server.join();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12374 次 |
| 最近记录: |