Apache CXF集成OData遇404错误:访问/hello无响应且无控制台报错
路径配置重复导致叠加
你的配置类CxfConfig里设置了factoryBean.setAddress("/hello"),同时服务类HelloService又添加了@Path("/hello"),这会让实际访问路径变成/hello/hello,而你访问的是/hello,自然返回404。
解决方法:二选一修改即可——要么把配置类里的setAddress("/"),保留服务类的@Path("/hello"),访问http://localhost:8080/hello;要么去掉服务类的@Path("/hello"),保留配置类的setAddress("/hello"),同样访问http://localhost:8080/hello。检查CXF依赖完整性
确保项目依赖中引入了Spring Boot集成CXF的正确依赖,以Maven为例,需要在pom.xml中添加:<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxrs</artifactId> <version>你的CXF对应版本号</version> </dependency>依赖缺失可能导致CXF服务未被正确初始化,但控制台不会输出明显报错。
确认CXF Servlet已注册
部分Spring Boot版本需要手动注册CXF Servlet,否则请求无法被CXF处理链接收。可以在配置类中添加如下Bean:@Bean public ServletRegistrationBean<CXFServlet> cxfServlet() { ServletRegistrationBean<CXFServlet> registration = new ServletRegistrationBean<>(new CXFServlet(), "/*"); registration.setLoadOnStartup(1); return registration; }检查Spring组件扫描范围
确认CxfConfig和HelloService所在的包,被Spring Boot主启动类的@SpringBootApplication注解覆盖扫描。如果主启动类包范围未包含这两个类的包,需要添加@ComponentScan指定扫描路径。排查应用上下文路径
如果在配置文件(如application.properties)中设置了server.servlet.context-path(比如server.servlet.context-path=/myapp),实际访问路径需要加上这个上下文前缀,比如调整路径后访问http://localhost:8080/myapp/hello。
内容的提问来源于stack exchange,提问作者Nismath vi

