博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Embedded tomcat 7 servlet 3.0 annotations not working--转
阅读量:6524 次
发布时间:2019-06-24

本文共 5530 字,大约阅读时间需要 18 分钟。

Question:

I have a stripped down test project which contains a Servlet version 3.0, declared with annotations like so:

@WebServlet("/test") public class TestServlet extends HttpServlet { private static final long serialVersionUID = -3010230838088656008L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.getWriter().write("Test"); response.getWriter().flush(); response.getWriter().close(); } }

I also have a web.xml file like so:

testServlet
g1.TestServlet
testServlet
/testWebXml

I've tried to make a JUnit test using Embedded Tomcat 7. When I start the Embedded Tomcat I can only access the servlet via the url-pattern declared in web.xml (/testWebXml). If I try to access it via the url-pattern declared via annotation (/test) it sais 404 page not found.

Here's the code for my test:

String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); tomcat.addWebapp("/jerseyTest", new File(webappDirLocation).getAbsolutePath()); tomcat.start(); tomcat.getServer().await();

Just to make sure I've set up my project correctly, I've also installed an actual Tomcat 7 and deployed the war. This time, both web.xml declared url and annotation url for my servlet work ok.

So my question is: does anyone know how to make Embedded Tomcat 7 take into account my Servlet 3.0 annotations?

I should also state that it's a Maven project, and the pom.xml contains the following dependencies:

org.apache.tomcat
tomcat-catalina
7.0.29
test
org.apache.tomcat.embed
tomcat-embed-core
7.0.29
provided
org.apache.tomcat
tomcat-jasper
7.0.29
test
junit
junit
4.8.1
test

== UPDATE ==

Here's an issue that seems similar to this (except the Servlet 3.0 annotation that is not working is on Listener, not Servlet), which has a suggested fix:

I've tried it and it didn't work:

Changed the Embedded Tomcat start code to:

String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); tomcat.enableNaming(); tomcat.setPort(8080); Context ctx = tomcat.addWebapp(tomcat.getHost(), "/embeddedTomcat", new File(webappDirLocation).getAbsolutePath()); ((StandardJarScanner) ctx.getJarScanner()).setScanAllDirectories(true); tomcat.start(); tomcat.getServer().await();

Other things I've tried, also without success:

  • specifically setting metadata-complete="false" in web.xml "web-app" tag

  • updating the Maven dependencies to version 7.0.30

  • debugging the org.apache.catalina.startup.ContextConfig class. There's code there that checks for @WebServlet annotations, it's just that it never gets executed (line 2115). This may be a good way to get to the root of the issue, but the class is pretty big, and I don't have time to do this now. Maybe if someone would be willing to look how this class works, and under which conditions (config params) does it get to correctly check your project's classes for that annotation, it might get to a valid answer.

 

Answer:

Well I finally solved it by looking in the Tomcat7 sources, namely in the unit tests that deal with EmbeddedTomcat and servlet 3.0 annotations.

Basically, you must start your Embedded Tomcat 7 like this to make it aware of your annotated classes:

String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); StandardContext ctx = (StandardContext) tomcat.addWebapp("/embeddedTomcat", new File(webappDirLocation).getAbsolutePath()); //declare an alternate location for your "WEB-INF/classes" dir: File additionWebInfClasses = new File("target/classes"); VirtualDirContext resources = new VirtualDirContext(); resources.setExtraResourcePaths("/WEB-INF/classes=" + additionWebInfClasses); ctx.setResources(resources); tomcat.start(); tomcat.getServer().await();

For the sake of clarity I should mention that this works for a standard Maven project where your "web resources" (such as static and dynamic pages, WEB-INF directory etc) are found in:

[your project's root dir]/src/main/webapp

and your classes get compiled into

[your project's root dir]/target/classes

(such that you'd have [your project's root dir]/target/classes/[some package]/SomeCompiledServletClass.class)

For other directories layouts, these locations need to be changed accordingly.

==== UPDATE: Embedded Tomcat 8 ====

Thanks to @kwak for noticing this.

The APIs have changed a bit, here how the above example changes when using Embedded Tomcat 8:

String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); StandardContext ctx = (StandardContext) tomcat.addWebapp("/embeddedTomcat", new File(webappDirLocation).getAbsolutePath()); //declare an alternate location for your "WEB-INF/classes" dir: File additionWebInfClasses = new File("target/classes"); WebResourceRoot resources = new StandardRoot(ctx); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/")); ctx.setResources(resources); tomcat.start(); tomcat.getServer().await();

原文地址:http://stackoverflow.com/questions/11669507/embedded-tomcat-7-servlet-3-0-annotations-not-working/12688842#12688842

 

转载地址:http://ebjbo.baihongyu.com/

你可能感兴趣的文章
linux下配置网络环境
查看>>
java Windows7 下环境变量设置
查看>>
NBU异构还原Oracle完整备份的一些总结
查看>>
freeBSD安装详细讲解
查看>>
WSFC2016 VM弹性与存储容错
查看>>
文档管理,文本编辑控件TX Text Control .NET for WPF
查看>>
复习 Python 匿名函数 内建函数
查看>>
Security Identifiers | Win SRV2016 SID Change 修改
查看>>
看看来自日本的扫描,做网站需要注意的
查看>>
JDK 1.7+Android SDK+IntelliJ IDEA 13+Genymotion 安卓开发环境部署
查看>>
钓鱼邮件***防范指南
查看>>
session_start()放置位置的不正确引发的ROOT常量 未定义的错误
查看>>
如何设定VDP同时备份的任务数?
查看>>
ipsec的***在企业网中的经典应用
查看>>
过来人谈《去360还是留在百度?》
查看>>
mysql备份工具innobackupex,xtrabackup-2.1安装,参数详解
查看>>
【复制】slave筛选复制之二(create/drop table语句)
查看>>
Movie Store OpenCart 自适应主题模板 ABC-0249
查看>>
mytop-MySQL监控工具
查看>>
RedHat linux YUM本地制作源
查看>>