This blog is part of the series, which needs a Helloapp to demonstrate the workflow and the tools features. I have built this app with Java using Spring Boot to build the MVC framework. This blog demonstrate the steps to build a standard Java app with view model to expose URI paths to external users.
Setup app environment
Planned to use the personal macos laptop as the build environment. Following tools have been installed and configured for the app development.
Install Java 17 and Maven to the workstation
I have used Homebrew to install maven. Maven can be installed with brew install maven. Java17 has been installed usng the binary from the Eclipse Temurin™ project.
We have to setup the JAVA_HOME environment variable to enable java and maven globally on the workstation.
Install other Java and value added extensions along with Spring Boot extension in VSCode.
Build Spring Boot project
We can use the Spring Initializr to create the project blueprint and add the required dependencies to get them added to the pom.xml file.
Spring Boot Dependencies added to the project :
Spring Web: Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.
Spring Boot DevTools: Provides fast application restarts, LiveReload, and configurations for enhanced development experience.
Thymeleaf: A modern server-side Java template engine for both web and standalone environments. Allows HTML to be correctly displayed in browsers and as static prototypes.
pom file
Project structure
In this project, build the src folder structure as below :
Build the controller
Since the usecase is to just build the Helloapp, thus we would not add the models, services and repository sections, as it’s not needed for this project. The controller would be built to expose the methods to model the view template with the content and control the view layer.
This is the controller.java file, which we use to create 2 methods for index and the about page. We expose the greeting and the username on the index.html page, via 2 sources – (1) from application.properties as the default value and (2) request-params which can be used to override the default is required. We read the greet and user from properties file via @value annotation into the variable which gets passed to the model.
The controller is built to also access the app version from the properties file and display on the index and the about page of the application.
Properties file
The properties can be used further to expose other project related variables. As of now, we have exposed server port, logging level and the app related custom variables.
View layer
I have used the Materialize framework to build the web frontend pages. To expose the web pages, we would add the index.html and about.html pages to the helloappjava/src/main/resources/templates path of the project. The project template files can be found here.
Static files
Since the usecase is to build a Helloapp which can be used for various poc purpose thus there might be a need to use this application offline (without internet). To address this point, we have maintained all the static assets (i.e. css, js, img and fonts) at helloappjava/src/main/resources/static. My efforts have been to keep this app as light as possible, but still have all the raw materials for extending this project, if needed in future.
We can visualize the static files in the tree structure below:
As we can see, I have used the fontawesome (The iconic font and CSS toolkit) css and fonts in the project.
Build and run app
We can test the application with the run/debug extension in VSCode or in command prompt as below.
Build and package the java application with mvn clean package into JAR file and launch the application locally exposed on port 8080 as below :
Wrapping Up
With the above changes and configurations, the application is ready to be used for various use cases.
This is an ongoing project and more enhancements (eg. Spring Boot Actuator for monitoring and swagger for APIs) would be added later. I would update the github project accordingly.