为什么Spring Boot应用在Azure Web应用上部署后无法工作?

问题描述 投票:0回答:1

谁能帮助我解决404错误,当我试图访问部署在azure上的api Url.由于管理员权限,无法安装azure CLI,如果现在.所以我已经按照以下步骤。

第1步:创建Spring引导应用程序,从HelloController返回字符串 "Hello",并在本地测试代码,它是向上和运行。

第2步:在azure门户上创建web应用,并配置本地git仓库。

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

第四步:在创建的Webapps文件夹中,我已经将war文件重命名为ROOT.war。

步骤4:运行以下命令。

git status

git add .

git commit -m "Test"

git push origin master

第四步:进入azure门户,检查ROOT文件夹和War File,成功部署。点击这里查看截图

步骤6:当我点击网址时,我得到404错误。点击这里查看错误图片

控制器的代码片段。

 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中,Configuration是通过在spring boot中运行maven插件(goal:mvn-webapp:config)最后创建的,该插件创建了默认的配置,然后我在配置标签中映射了与我在我的azure portal中创建的相同的值,目标只是检查创建了哪些配置属性。

<?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>

应用程序.属性。

server.forward-headers-strategy=FRAMEWORK。

错误。在 Azure 中的 Diagonose 和 Section 下。

点击这里查看错误图像

点击这里查看错误图片

java azure spring-boot
1个回答
0
投票

要部署一个春天的引导应用程序到Azure应用服务,它是建议部署的jar文件。

我按照 本教程 就可以正常工作了。如果你总是看到默认页面,你需要添加一个web.config文件。

enter image description here

wwwroot目录下的文件。

enter image description here

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>
© www.soinside.com 2019 - 2024. All rights reserved.