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.
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.
-
Open Spring Initializr to create a new project.
-
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. -
Click Generate.
-
Unzip the project and open it in any IDE of your choice.
-
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.
copypackage 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!"; } }
- 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:
copypackage 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.
- You can run this application on your local machine by executing the following command from your terminal:
copy./mvnw spring-boot:run
- 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.
-
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 -
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.
-
Deploy the app service to the console.
-
You can then configure the startup command given below from the console:
copyjava -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:
-
Open Spring Initializr to create a new project.
-
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. -
Click Generate.
-
Unzip the project and open in any IDE of your choice.
-
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.
copypackage 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!"; } }
- 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>
- You can run this application on your local machine by executing the following command from your terminal:
copy./mvnw spring-boot:run
- 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.
-
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 -
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.
-
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
Yes
No
Send your feedback to us