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

Spring Boot部署至WebSphere 9后REST接口404问题求助

Spring Boot 3.1.5部署WebSphere 9后REST接口404问题解决

部署Spring Boot 3.1.5 WAR包到WebSphere 9成功,但访问/test接口时出现404错误:Error 404: java.io.FileNotFoundException: SRVE0190E: File not found: /test。以下是当前的配置和代码,需要调整以下配置才能正常访问接口:

当前配置与代码

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

REST控制器代码

@RestController
public class restCont {

    @GetMapping("/test")
    public String test() {
        return "test";
    }
}

解决步骤

1. 添加Servlet初始化类

WebSphere作为传统Java EE容器,需要显式初始化Spring Boot应用。创建继承SpringBootServletInitializer的类:

package com.example.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class); // 替换为你的主启动类
    }
}

2. 确保组件扫描范围正确

检查主启动类(如DemoApplication)的@SpringBootApplication注解,默认会扫描当前包及其子包。如果控制器不在该范围内,添加@ComponentScan指定路径:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example", "com.example.demo"})
public class DemoApplication {

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

3. 调整WebSphere类加载策略

WebSphere默认的类加载顺序会导致Spring Boot Servlet无法正确加载:

  • 登录WebSphere控制台,找到部署的应用
  • 进入类加载和更新检测设置
  • 将类加载顺序改为类加载器的父类加载器最后(PARENT_LAST)
  • 保存后重启应用

4. 确认访问路径包含上下文根

WebSphere默认以WAR包名为上下文根(如WAR包为demo-0.0.1-SNAPSHOT.war,上下文根为/demo-0.0.1-SNAPSHOT),正确访问路径应为http://<server>:<port>/<context-root>/test。如需自定义上下文根,在pom.xml中添加:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <warName>demo</warName> <!-- 上下文根变为/demo -->
            </configuration>
        </plugin>
    </plugins>
</build>

5. 验证版本兼容性

Spring Boot 3.1.x要求Java 17,需确保WebSphere 9版本为9.0.5.10及以上(该版本开始支持Java 17),否则需升级WebSphere。

内容的提问来源于stack exchange,提问作者MIX 2000

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 03:11:23