Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>Basically, you must start your Embedded Tomcat 7 like this to make it aware of your annotated classes:</p> <pre><code>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(); </code></pre> <p>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:</p> <p>[your project's root dir]/src/main/webapp </p> <p>and your classes get compiled into </p> <p>[your project's root dir]/target/classes</p> <p>(such that you'd have [your project's root dir]/target/classes/[some package]/SomeCompiledServletClass.class)</p> <p>For other directories layouts, these locations need to be changed accordingly.</p> <p>==== UPDATE: Embedded Tomcat 8 ====</p> <p>Thanks to @kwak for noticing this.</p> <p>The APIs have changed a bit, here how the above example changes when using Embedded Tomcat 8:</p> <pre><code>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(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload