Embedded Jetty cannot find annotated servlet

Short: I have a project that provides a war artifact that includes a servlet with annotations, but not web.xml. If I try to use war at the pier, I always get only a list of directories of military content, but not servlet execution.

Any idea?

Long story: My servlets look like this:

package swa; import java.io.IOException; import java.io.PrintWriter; 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( asyncSupported = false, urlPatterns={"/*"}) public class ServletX extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); // Actual logic goes here. PrintWriter out = response.getWriter(); out.println("<h1>Hi there..</h1>"); } } 

So I don’t think anything special. When I use mvn jetty:run , everything is fine. Making sure that the project is packaged in a military archive.

This military archive is used in another project, which should establish a berth in the code. Here's how to do it:

  String jettyPort = properties.getProperty("jetty.port", "8080"); Server server = new Server(); ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory()); httpConnector.setPort(Integer.parseInt(jettyPort)); httpConnector.setAcceptQueueSize(2); httpConnector.setHost("0.0.0.0"); httpConnector.setIdleTimeout(30000); server.setConnectors(new Connector[] { httpConnector }); WebAppContext wacHandler = new WebAppContext(); wacHandler.setContextPath("/admin"); wacHandler.setWar("swa-0.0.1-SNAPSHOT.war"); wacHandler.setConfigurationDiscovered(true); server.setHandler(wacHandler); server.start(); 

During this project, magazines tell me that a war has been found. But if I open the url http://localhost:8080/admin , I only see a list of military content (instead of "Hello there").

Can someone point me to my refusal?

+2
java annotations servlets jetty embedded-jetty
source share
2 answers

You need to correctly define WebAppContext configurations (and in the correct order).

  wacHandler.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebInfConfiguration(), new WebXmlConfiguration(), new MetaInfConfiguration(), new FragmentConfiguration(), new EnvConfiguration(), new PlusConfiguration(), new JettyWebXmlConfiguration() }); 

Remember to add jetty-annotations.jar .

This is an example from EmbedMe.java for embedded-jetty to use with Servlet 3.1 found in

https://github.com/jetty-project/embedded-servlet-3.1/

+4
source share

In addition to adding the necessary configurations, as indicated in the above answer, you must force Jetty to scan the current compiled project classes that Jetty ignores by default. To do this, simply call the following in WebAppContext:

 context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/classes/.*") 

See the full example here (in Kotlin): https://github.com/mvysny/vaadin-on-kotlin/blob/master/vok-example-crud/src/test/java/com/github/vok/example/ crud / Server.kt

This works amazingly and detects all @WebServlets, @WebListeners and others.

+1
source share

All Articles