Tomcat中无法访问CXF服务:HTTP 500内部服务器错误排查
I’ve dealt with this exact CXFServlet initialization issue a few times, so let’s break down the likely causes and fixes based on your setup:
Key Observations from Your Problem
- Tomcat returns HTTP 500 when accessing
/CXF-Maven/services - Root cause:
Servlet.init() for servlet [CXFServlet] raised an exception - Your
cxf-servlet.xmlconfig defines a JAX-WS server pointing toCXF-Maven.DemoClass
Common Fixes to Try
1. Verify Your Service Implementation Class
First, check if DemoClass is properly set up as a JAX-WS service:
- Ensure the class has the
@WebServiceannotation: Without this, CXF can’t recognize it as a valid web service. Example:// Fix the package name to follow Java conventions (lowercase preferred) package cxfmaven; import javax.jws.WebService; @WebService public class DemoClass { public String hallo(String name) { return "Hallo " + name; } } - Check the class path in
cxf-servlet.xml: The valueCXF-Maven.DemoClassuses an uppercase package name, which is non-standard and might be causing a class-not-found error. Update it to match your actual package structure (e.g.,<bean class="cxfmaven.DemoClass"/>). - Confirm the class exists in
WEB-INF/classes: After compiling, make sure theDemoClass.classfile is present in the correct package directory under your war’sWEB-INF/classesfolder.
2. Fix Missing or Conflicting CXF Dependencies
CXF requires specific jars to run in Tomcat. If these are missing, the servlet will fail to initialize:
- If you’re using Maven, add these core dependencies to your
pom.xml(adjust versions for compatibility):<dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.24</version> </dependency> </dependencies> - Ensure all these jars are present in your war’s
WEB-INF/libdirectory. Missing jars are one of the most common causes of this 500 error.
3. Correct Web.xml Configuration
Make sure your web.xml properly maps the CXFServlet and loads the CXF Spring config:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- Load CXF Spring configuration --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/cxf-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Map CXFServlet to handle /services/* requests --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
Without the ContextLoaderListener, Spring won’t load your cxf-servlet.xml config, so the CXFServlet can’t find your service bean.
4. Use the Correct Service URL
Once everything is set up, you won’t get content at http://localhost:8080/CXF-Maven/services by default. Instead, access the specific WSDL URL:
http://localhost:8080/CXF-Maven/services/hallo?wsdl
This should load your service’s WSDL if all configurations are correct.
Final Notes
Start with checking the @WebService annotation and class path in cxf-servlet.xml—those are the easiest fixes. Then verify your dependencies and web.xml config. If you’re still stuck, check Tomcat’s full logs (not just the 500 error page) for a root cause exception like ClassNotFoundException or NoClassDefFoundError—those will point you directly to the problem.
内容的提问来源于stack exchange,提问作者Abdo Bmz

