You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tomcat中无法访问CXF服务:HTTP 500内部服务器错误排查

Troubleshooting CXFServlet 500 Error in Tomcat

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.xml config defines a JAX-WS server pointing to CXF-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 @WebService annotation: 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 value CXF-Maven.DemoClass uses 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 the DemoClass.class file is present in the correct package directory under your war’s WEB-INF/classes folder.

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/lib directory. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:14:23