Embedded tomcat 7 servlet 3.0 annotations not working

后端 未结 4 707
孤独总比滥情好
孤独总比滥情好 2020-12-01 03:19

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

    @WebServlet(\"/test\")
public class TestServ         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 03:27

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

提交回复
热议问题