Search code examples
tomcatjax-rshttp-status-code-404

JAX-RS webapp deployed to Tomcat returns HTTP 404 error while it works fine in Eclipse


i'm trying o deploy a Jax-RS webapp on Tomcat using Apache CXF implementation. Its a simple webapp for an exercise for university. When i do it using Eclipse all works fine, the applicaion gets recognized and i can reach it using web browser. If i try to generate a war and deploy it in any other way, it gets recognized but i can't reach it in any way, what am i missing?

service implementation

service interface, application class my directory structure in webapps directory is: structure

i try to reach the webapp at "localhost:8080/es4.3/eshop/items" but i get this error: error

im using old versions of Tomcat because that's what's our teacher told us, because of some incompatibility between Apache CXF and jax-ws due to its tranisition to jakarta


Solution

  • I think I have an answer to this problem now.

    In another implementation I did using Jersey I had the same problem, but studying how tomcat works I manage to solve it.

    I didn't properly understand what Tomcat is, so for the ones who share this problem I'm gonna go for a full explanation.

    Basically Tomcat is just a Servlet container, so it doesn't support at all JAX-RS neither the full JavaEE or JakartaEE, but just the Servlet specification.

    So Jersey provides a so called "jersey-servlet", this servlet (that you can just add to your project for example by adding it to your project dependencies) it's automatically recognized from Tomcat (because the most recent implementations of Tomcat, I think starting from Tomcat 8, supports Servlet 3.0 specification, which provides a set of annotations to automatically configure and start servlets in any container). Then it's this servlet responsibility to "manage the jax-rs part" of your Application, managing http request and routing them to the right service annotated with jax-rs annotation.

    So, in the previous exercise I think I was missing the "jersey-servlet" equivalent for CXF.

    Now, why was it working when I deployed it with Eclipse? That's still a question, but I can try to provide an answer, on which I'm not entirely sure. The "eclipse version" of this exercise was made through eclipse "dynamic web Project" and with a CXF dependency downloaded on my system from the website. So maybe the dynamic web project option, or that version of the CXF library, provided, unbeknownst to me, that "Jersey-servler" equivalent for CXF.