Spring Boot

Spring Boot is built on top of the popular Spring framework, and includes a wide range of enhancements that allow you to easily build production-grade microservices, web applications, and standalone Spring projects with reduced development efforts. Spring Boot typically includes embedded HTTP servers like Jetty or Apache Tomcat, without the complex XML configurations of Spring.

Note: When you export a Spring Boot app as a JAR file in the example below, you can retain the embedded server included in it. However, when you export the Spring Boot app as a WAR file, you will need to exclude the embedded server included in it, if you initialize it as Java WAR in AppSail. This is because AppSail offers native support for WAR files and bundles them with the Jetty server by default.

Spring Boot- JAR

This example illustrates the steps to build a sample Spring Boot app, export it as a JAR file, and deploy it to AppSail.

  1. Open Spring Initializr to create a new project.

  2. Choose the following configurations for your project:

    a. Project - Maven Project
    b. Language - Java
    c. Spring Boot Version - 2.7.10
    d. Package - JAR
    e. Java Version - 8
    f. Click Add Dependencies and select Spring Web.

  3. Click Generate.

  4. Unzip the project and open it in any IDE of your choice.

  5. Add necessary controllers to the project. For example, create a file named DemoController.java in the main Java file’s directory of your project, and add the controller specified below in the file.

    
copy
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @GetMapping("/") public String index() { return "Greetings from Spring Boot!"; } }

  1. Catalyst will check the port configured for listening with the environment variable X_ZOHO_CATALYST_LISTEN_PORT and the app must listen to that port on all interfaces (host 0.0.0.0). Therefore, you will need to customize the port. You can do this by creating a file named ServerPortCustomizer.java in the main Java file’s directory of your project, and adding the code specified below in it:
    
copy
package com.example.demo; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.stereotype.Component; @Component public class ServerPortCustomizer implements WebServerFactoryCustomizer { @Override public void customize(ConfigurableWebServerFactory factory) { String port = System.getenv("X_ZOHO_CATALYST_LISTEN_PORT"); int listenPort; if(port != null && !port.isEmpty()) { listenPort = Integer.parseInt(System.getenv("X_ZOHO_CATALYST_LISTEN_PORT")); } else { listenPort = 9000; } factory.setPort(listenPort); } }

You can now save the application and run it in Catalyst.

  1. You can run this application on your local machine by executing the following command from your terminal:
    
copy
./mvnw spring-boot:run
  1. You can upload the application to Catalyst with this command:
    
copy
./mvnw clean package

This will generate the JAR file of the application in the target folder.

  1. You can now initialize an AppSail service in the same directory from the CLI or add it in an existing project directory. The app’s source must be your application’s directory. Provide the following values while initializing the app service:

    Stack: Java 8
    Platform: Java SE

  2. Ensure the JAR file is added in the build directory you specified during initialization. Catalyst will automatically ZIP your app file during deployment to the remote console.

  3. Deploy the app service to the console.

  4. You can then configure the startup command given below from the console:

    
copy
java -jar demo-0.0.1-SNAPSHOT.jar

You can also configure this in the app-config.json file before deploying.


Access the deployed app service from its endpoint URL.



Spring Boot - WAR

This example explains the steps to build a sample Spring Boot app, export it as a WAR file, and deploy it to AppSail:

  1. Open Spring Initializr to create a new project.

  2. Choose the following configurations for your project:

    a. Project - Maven Project
    b. Language - Java
    c. Spring Boot Version - 2.7.10
    d. Package - WAR
    e. Java Version - 8
    f. Click Add Dependencies and select Spring Web.

  3. Click Generate.

  4. Unzip the project and open in any IDE of your choice.

  5. You must now add necessary controllers to the project. For example, create a file named DemoController.java in the main Java file’s directory of your project, and add the controller specified below in the file.

    
copy
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @GetMapping("/") public String index() { return "Greetings from Spring Boot WAR!"; } }

  1. You must now modify the pom.xml file in your project to exclude the embedded Tomcat server while generating the WAR file, as explained in the overview. You can do this by adding the snippet given below in the spring-boot-maven-plugin enclosure in the XML file:
    
copy
<configuration> <mainClass>${main_class_name}</mainClass> <classifier>exec</classifier> </configuration>

  1. You can run this application on your local machine by executing the following command from your terminal:
    
copy
./mvnw spring-boot:run

  1. You can upload the application to Catalyst with this command:
    
copy
./mvnw clean package

This will generate the WAR file of the application in the target folder.

  1. You can now initialize an AppSail service in the same directory from the CLI or add it in an existing project directory. The app’s source must be your application’s directory. Provide the following values while initializing the app service:

    Stack: Java 8
    Platform: Java WAR

  2. Ensure the WAR file is added in the build directory you specify during initialization. Catalyst will automatically ZIP your app file during deployment to the remote console.

  3. Deploy the app service to the console.


Access the deployed app service from its endpoint URL.

Last Updated 2023-11-14 13:20:49 +0530 +0530