【问题标题】:Why Spring Boot Application is not working after Deployment on Azure Web app为什么在 Azure Web 应用程序上部署后 Spring Boot 应用程序不起作用
【发布时间】:2020-08-18 00:07:20
【问题描述】:

当我尝试访问部署在 azure 上的 api Url 时,谁能帮我解决 404 错误。由于管理员权限,无法像现在一样安装 azure CLI。所以我按照以下步骤操作。

第 1 步: 创建了从 HelloController 返回字符串“Hello”的 Spring Boot 应用程序,并在本地测试了代码,它已启动并运行。

第 2 步: 在 Azure 门户上创建 Web 应用并配置本地 git 存储库。

第 3 步: 在我的本地,我复制了本地的 git url 并进行了 git 克隆。

第 4 步: 在创建的 Webapps 文件夹中,我通过将其重命名为 ROOT.war 来放置 war 文件

第 4 步: 运行以下命令:

混帐状态

git 添加。

git commit -m"测试"

git push origin master

第 4 步: 进入azure portal,查看ROOT文件夹和War File,部署成功。 Click here For the Screenshot

第 6 步: 当我点击网址时,我收到 404 错误: Click here for Error Image

控制器的代码片段:

 package io.javabrains.springbootstarter;


 import org.springframework.web.bind.annotation.RequestMapping;

 import org.springframework.web.bind.annotation.RestController;

 @RestController
 public class HelloController {
      @RequestMapping("/hello")
    public String hello() {
        return "Hello World RESTful with Spring Boot";
     }  
   }

Spring Boot Starter 的代码片段:

package io.javabrains.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CourseApiApp {

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

   }

 }

Pom.xml:

在 Pom.xml 中,最后通过在 spring boot 中运行创建默认配置的 maven plugin(goal:mvn-webapp:config) 创建了配置。然后我在我创建的配置标签中映射了相同的值在我的天蓝色门户中。目标是检查创建了哪些配置属性。

<?xml version="1.0" encoding="UTF-8"?>

<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 http://maven.apache.org/xsd/maven- 
 4.0.0.xsd">  
 <modelVersion>4.0.0</modelVersion>  
 <groupId>io.javabrains.springbootquickstart</groupId>  
 <artifactId>course-api-test</artifactId>  
 <version>0.0.1-SNAPSHOT</version>  
 <packaging>war</packaging>  
 <name>Java Brains Course Api</name>  
 <parent> 
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-parent</artifactId>  
 <version>2.1.3.RELEASE</version>  
 <relativePath/>  
 <!-- lookup parent from repository --> 
 </parent>  
 <dependencies> 
 <!-- WEB -->  
 <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>  
 <properties> 
 <java.version>1.8</java.version> 
 </properties>  
 <build> 
   <plugins> 
    <plugin> 
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-maven-plugin</artifactId> 
  </plugin>  
  <plugin> 
    <groupId>com.microsoft.azure</groupId>  
    <artifactId>azure-webapp-maven-plugin</artifactId>  
    <version>1.7.0</version>  
    <configuration>
      <schemaVersion>V2</schemaVersion>
      <resourceGroup>first-azure-application-28minutes</resourceGroup>
      <appName>CourseapiTest</appName>
      <pricingTier>F1</pricingTier>
      <region>South Central US</region>
      <appSettings>
      <property>
      <name>JAVA_OPTS</name>
      <value>-Dserver.port=80</value>
      </property>
      </appSettings>
      <runtime>
        <os>windows</os>
        <javaVersion>jre8</javaVersion>
        <webContainer>TOMCAT 9</webContainer>
      </runtime>
      <deployment>
              <resources>
                    <resource>
                        <directory>${project.basedir}/target</directory>
                           <includes>
                              <include>*.war</include>
                           </includes>
                    </resource>
              </resources>
      </deployment>
    </configuration>
  </plugin> 
    </plugins> 
    </build> 
   </project>

application.properties:

server.forward-headers-strategy=FRAMEWORK

错误:在 Azure 中的对角线和部分下:

Click here for Error Image

Click here for Error Image

【问题讨论】:

    标签: java azure spring-boot


    【解决方案1】:

    要将spring boot app部署到Azure App Service,建议部署jar文件。

    我关注了this tutorials,它运行良好。如果总是看到默认页面,则需要添加 web.config 文件。

    wwwroot目录下的文件。

    web.config

    <?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="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\app.jar&quot;">
        </httpPlatform>
      </system.webServer>
    </configuration>
    

    【讨论】:

    • 谢谢。这就是失去的和平。在我的情况下,web.config 从未从 vscode 生成和上传。必须使用 FTP 手动创建和上传。
    猜你喜欢
    • 2020-03-05
    • 2014-04-28
    • 1970-01-01
    • 2017-09-30
    • 2023-03-16
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多