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

Spring MVC转Spring Boot部署WebLogic遇DispatcherServletPath缺失错误

解决Spring Boot 2.6.4部署到WebLogic 14.1.1.0的两类问题

一、解决org.jboss.weld.module.web.servlet.WeldInitialListener空指针警告

WebLogic 14内置Weld(CDI规范实现),Spring Boot应用部署时会和CDI自动初始化逻辑冲突触发警告,直接禁用WebLogic的隐式CDI支持即可:

  • 在weblogic.xml中添加以下配置:
<weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/14.1/weblogic-web-app.xsd">
    <!-- 已有配置保留 -->
    <wls:container-descriptor>
        <!-- 优先使用应用内Spring包,避免与WebLogic自带类冲突 -->
        <wls:prefer-application-packages>
            <wls:package-name>org.springframework.*</wls:package-name>
        </wls:prefer-application-packages>
        <!-- 禁用WebLogic隐式CDI初始化 -->
        <wls:disable-implicit-cdi>true</wls:disable-implicit-cdi>
    </wls:container-descriptor>
</weblogic-web-app>

二、解决DispatcherServletPath Bean找不到导致的部署失败

该问题源于Spring Boot自动配置在WebLogic容器下的适配缺失,需通过以下步骤修复:

1. 显式声明DispatcherServletPath Bean

创建Web配置类,手动提供该Bean:

import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebMvcConfig {
    @Bean
    public DispatcherServletPath dispatcherServletPath() {
        // 替换为项目实际的Servlet映射路径(原Spring MVC项目的url-pattern)
        return () -> "/";
    }
}

2. 同步Spring Boot Servlet配置

在application.properties中指定与上述Bean一致的Servlet路径:

spring.mvc.servlet.path=/

3. 修正Gradle依赖配置

排除内置Tomcat依赖(WebLogic作为容器),并正确引入Servlet API:

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.yourcompany'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // 移除内置Tomcat容器
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    // 引入Servlet API(WebLogic已自带,用providedRuntime避免冲突)
    providedRuntime 'javax.servlet:javax.servlet-api:4.0.1'
    // 其他业务依赖按需添加
}

// 打包为WAR包适配WebLogic
war {
    enabled = true
}

4. 确保启动类继承SpringBootServletInitializer

部署到传统Servlet容器时,启动类必须继承该类才能让WebLogic正确初始化Spring容器:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class YourApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(YourApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

5. 清理web.xml冲突配置

移除原Spring MVC web.xml中手动配置的DispatcherServlet等重复内容,让Spring Boot自动处理;若需保留部分配置,需确保与Spring Boot自动配置无冲突。


内容的提问来源于stack exchange,提问作者Anudeep T

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 01:05:04