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

Azure Web App部署Spring Boot(Kotlin)后返回404错误求助

解决Azure App Service部署Spring Boot应用后404问题

问题场景

  • 基于Kotlin开发的Spring Boot 3.1.10应用,运行时栈为Java 17 Tomcat
  • 通过Terraform创建Azure App Service后显示默认页面,使用GitHub Actions部署jar包后访问出现404 Not Found错误
  • 已尝试设置环境变量ApplicationInsight_JAVA_EXTENSION = 3.4.10,问题未解决

用户提供的GitHub Actions配置(cicd.yml):

name: hello

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Sources
      uses: actions/checkout@v3
    - name: Setup Java
      uses: actions/setup-java@v3
      with:
        distribution: 'temurin'
        java-version: '17'

    - name: Build Project
      run: mvn clean install -DskipTests

    - name: Upload Artifact for Deplyoment Job
      uses: actions/upload-artifact@v2
      with:
        name: microservice
        path: ${{ github.workspace}}/target/*.jar

  deploy:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Download Artifacts from build Job
        uses: actions/download-artifact@v2
        with:
          name: microservice

      - name: Deploy Microservice to Azure App Service
        uses: azure/webapps-deploy@v2
        with:
          app-name: abc-01
          publish-profile: ${{secrets.AZURE_PUBLISH_PROFILE}}
          package: '*.jar'

排查与解决步骤

1. 确认App Service运行时与启动配置

  • 验证运行时栈:在Azure Portal的App Service配置->常规设置中,确认运行时栈是Java 17 (Tomcat),Spring Boot 3.1.10与Java 17兼容,但需确保Azure提供的Tomcat版本支持Spring Boot 3.x。
  • 设置启动命令:对于可执行jar包部署,需明确指定启动逻辑:
    • Linux环境:在配置的启动命令中填入java -jar /home/site/wwwroot/*.jar
    • Windows环境:若未自动生成web.config,手动在site/wwwroot下创建该文件,内容如下:
      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
        <system.webServer>
          <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
          </handlers>
          <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
                       arguments="-jar &quot;%HOME%\site\wwwroot\<你的jar包全称>.jar&quot;">
          </httpPlatform>
        </system.webServer>
      </configuration>
      
  • 检查部署路径:通过Kudu工具(https://<你的应用名>.scm.azurewebsites.net/)查看site/wwwroot目录,确认jar包已上传至该目录,且无同名冲突文件。

2. 验证GitHub Actions部署流程

  • 检查部署日志:查看GitHub Actions中Deploy Microservice to Azure App Service步骤的日志,确认是否显示“Successfully deployed web package to App Service”,同时确认*.jar是否匹配到正确的文件。
  • 验证本地jar可用性:在本地环境运行java -jar <生成的jar包>,确认应用能正常启动并访问localhost:8080,排除jar包本身的编译或配置错误。
  • 优化Artifact路径:确认upload-artifact步骤的路径${{ github.workspace}}/target/*.jar正确,可添加验证步骤:
    - name: Verify Jar Exists
      run: ls -la target/*.jar
      working-directory: ${{ github.workspace }}
    

3. 排查环境变量与扩展冲突

  • 暂时移除ApplicationInsight_JAVA_EXTENSION环境变量,重新部署应用,确认是否因扩展版本不兼容导致启动失败。
  • 检查App Service的环境变量中是否设置了PORT,Spring Boot默认使用8080端口,Azure App Service会自动分配端口,需确保应用监听该端口(Spring Boot 3.x可通过server.port=${PORT:8080}配置)。

4. 查看应用日志定位问题

  • 在Azure Portal的App Service中打开日志流,实时查看应用启动日志,排查是否有端口冲突、依赖缺失、配置加载失败等报错。
  • 通过Kudu工具查看site/logs目录下的日志文件,重点关注Tomcat启动日志和Spring Boot应用日志,定位具体错误原因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 22:10:32