Search code examples
javaspring-bootvisual-studio-codeazure-appservice

Errors navigating to Azure App Service after deploying Spring Boot Java App to Linux App Service


I followed the steps here:

https://code.visualstudio.com/docs/java/java-webapp#_deploy-web-apps-to-the-cloud

and deployed the app under the "Complete" folder to Azure App Service. I had to change the packaging type from JAR to WAR in pom.xml so that the deployment was successful, otherwise, it was not deploying and was getting errors something as cannot deploy JAR artifact.

When I open the Azure App Service URL, I get the following error response:

HTTP Status 404 – Not Found
Type Status Report

Message The requested resource [/] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.78

I did enable the logs and diagnostics and I see the following error under App Service | AppServiceName | Diagnose and solve problems:

Container Crash
Displays application logs for why the container may have crashed
Application Errors that may have caused the container to crash were detected
2023-01-04T17:04:36.769939898Z Picked up JAVA_TOOL_OPTIONS: -Xmx716M -Djava.net.preferIPv4Stack=true 2023-01-04T17:04:37.193667111Z Error: LinkageError occurred while loading main class org.springframework.boot.loader.WarLauncher2023-01-04T17:04:37.197640485Z java.lang.UnsupportedClassVersionError: org/springframework/boot/loader/WarLauncher has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0 

Below is the pom.xml after modifying the java.version from 17 to 18 and packaging from jar to war:

<?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 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.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-boot-complete</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-complete</name>
    <description>Demo project for Spring Boot</description>
    <packaging>war</packaging>

    <properties>
        <java.version>18</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- tag::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- end::actuator[] -->

        <!-- tag::tests[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- end::tests[] -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

The JDK used was JDK 18 in VS Code on my local and the one deployed on Azure App Service was JDK 11. The Spring Boot configured in the pom.xml seems to be based on JDK 17. I repeated the deployment and chose App Service JDK 17 and Tomcat 10. This time I didn't see the error above in the diagnostic logs, but still, I see the same error response when I navigate to the app service URL.

Update 1

I followed the steps from this article and it worked from the first time: https://learn.microsoft.com/en-us/azure/app-service/quickstart-java?pivots=platform-linux-development-environment-maven&tabs=tomcat

I think the missing step from the first article is the configuration of Maven com.microsoft.azure:azure-webapp-maven-plugin. I need to check it out. The steps create the helloworld app with the following pom.xml. I think I cam compare and figure out the changes.

<?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/maven-v4_0_0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>example.demo</groupId>  
  <artifactId>helloworld</artifactId>  
  <packaging>war</packaging>  
  <version>1.0-SNAPSHOT</version>  
  <name>helloworld Maven Webapp</name>  
  <url>http://maven.apache.org</url>  
  <dependencies> 
    <dependency> 
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope> 
    </dependency> 
  </dependencies>  
  <build> 
    <finalName>helloworld</finalName>  
    <plugins>
      <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>2.5.0</version>
        <configuration>
          <schemaVersion>v2</schemaVersion>
          <subscriptionId>dd9aa0d1-f465-4cf3-a0e0-88aa1eb9a8c1</subscriptionId>
          <resourceGroup>helloworld-1672911087928-rg</resourceGroup>
          <appName>helloworld-1672911087928</appName>
          <pricingTier>P1v2</pricingTier>
          <region>centralus</region>
          <runtime>
            <os>Linux</os>
            <javaVersion>Java 11</javaVersion>
            <webContainer>Tomcat 8.5</webContainer>
          </runtime>
          <deployment>
            <resources>
              <resource>
                <directory>${project.basedir}/target</directory>
                <includes>
                  <include>*.war</include>
                </includes>
              </resource>
            </resources>
          </deployment>
        </configuration>
      </plugin>
    </plugins>
  </build> 
</project>

Any help to troubleshoot this problem would be greatly appreciated.


Solution

  • I checked the steps from:

    https://learn.microsoft.com/en-us/azure/app-service/quickstart-java?pivots=platform-linux-development-environment-maven&tabs=tomcat

    and, I combined the steps on the project I used earlier, mainly I ran the following commands:

    PS> cd complete
    PS> mvn com.microsoft.azure:azure-webapp-maven-plugin:2.5.0:config
    PS> mvn package azure-webapp:deploy
    

    Note that when creating an App Service, the selection of version Java 17 and web container Java SE is required to make it successful. Also, I was able to create the app service either using Maven command lines or using the Azure portal. I was unable to create the App Service with Java 17 from Azure Extension.

    The maven config adds the App Service setup to the pom.xml:

      <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>2.5.0</version>
            <configuration>
              <schemaVersion>v2</schemaVersion>
              <subscriptionId>dd9aa0d1-f465-4cf3-a0e0-88aa1eb9a8c1</subscriptionId>
              <resourceGroup>spring-boot-complete-1672914653291-rg</resourceGroup>
              <appName>spring-boot-complete-1672914653291</appName>
              <pricingTier>P1v2</pricingTier>
              <region>centralus</region>
              <runtime>
                <os>Linux</os>
                <javaVersion>Java 17</javaVersion>
                <webContainer>Java SE</webContainer>
              </runtime>
              <deployment>
                <resources>
                  <resource>
                    <directory>${project.basedir}/target</directory>
                    <includes>
                      <include>*.jar</include>
                    </includes>
                  </resource>
                </resources>
              </deployment>
            </configuration>
          </plugin>
        </plugins> 
      </build> 
    

    After following the above steps, it worked right away. I was able to make changes and right-click on the JAR file and deploy to the App Service again without any problem.

    It's clear the article from VS Code website is missing those steps.