部署Spring Boot 1.4.2到WebLogic 10.3遇NoClassDefFoundError求助
NoClassDefFoundError: javax/servlet/Registration$Dynamic问题 这个报错的核心原因很明确:WebLogic 10.3仅支持Servlet 2.5规范,而javax.servlet.Registration$Dynamic是Servlet 3.0才引入的API。Spring Boot的WebServicesAutoConfiguration自动配置类在启动时,会尝试使用Servlet 3.0的动态注册方式创建MessageDispatcherServlet,这就导致了在Servlet 2.5环境下找不到该类的错误。
下面是两种针对性的解决方案,你可以根据是否需要WebServices功能来选择:
方案一:直接禁用WebServices自动配置(无需WebServices功能时)
如果你的应用不需要WebServices相关功能,最简单的办法就是在Spring Boot主类上排除WebServicesAutoConfiguration,阻止它触发Servlet 3.0相关的代码:
@SpringBootApplication(exclude = WebServicesAutoConfiguration.class) public class YourSpringBootApplication { public static void main(String[] args) { SpringApplication.run(YourSpringBootApplication.class, args); } }
方案二:手动配置WebServices(需要WebServices功能时)
如果必须使用WebServices,需要手动配置MessageDispatcherServlet,适配Servlet 2.5的环境,同时禁用自动配置:
1. 排除自动配置
同样先在主类上排除WebServicesAutoConfiguration(同方案一的代码)。
2. 手动创建WebServices配置类
编写一个配置类,手动注册MessageDispatcherServlet,避免使用Servlet 3.0的动态API:
import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.ws.transport.http.MessageDispatcherServlet; @Configuration public class WebServicesConfig { @Bean public MessageDispatcherServlet messageDispatcherServlet(ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setTransformWsdlLocations(true); // 根据你的业务需求调整配置 return servlet; } @Bean public ServletRegistrationBean messageDispatcherServletRegistration(MessageDispatcherServlet servlet) { // 注册Servlet并指定请求映射路径 ServletRegistrationBean registration = new ServletRegistrationBean(servlet, "/ws/*"); registration.setName("messageDispatcherServlet"); return registration; } }
3. 确保Servlet API依赖正确配置
在pom.xml中添加Servlet 2.5的API依赖,并设置为provided(避免和WebLogic自带的Servlet API冲突):
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency>
4. 检查spring-boot-legacy配置
确认你已经正确引入spring-boot-legacy依赖,并且web.xml配置符合Servlet 2.5规范(这是spring-boot-legacy在Servlet 2.5容器运行的基础):
pom.xml依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-legacy</artifactId> <version>1.1.0.RELEASE</version> </dependency>
web.xml配置示例:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.yourpackage.YourSpringBootApplication</param-value> </context-param> <listener> <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextAttribute</param-name> <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
完成以上配置后,重新打包部署到WebLogic 10.3,应该就能解决这个类找不到的错误了。
内容的提问来源于stack exchange,提问作者osama yaccoub

