How difficult is it for a Java developer to create an application that needs to handle front-end technology? It’s a difficult question—and one that I hear every single day from back-end developers. So, how can we solve it in Spring world? With Spring MVC.
Spring MVC makes it easy to create stand-alone, production-grade, Spring-based applications that you can just run. In this post, we’ll show the perfect union between Spring MVC and MongoDB database, the NoSQL database most popular around the globe, running in the cloud on Platform.sh.
The initial web framework built on the Servlet API, Spring MVC has been included in the Spring Framework from day one. Spring Web MVC follows the Model-View-Controller design pattern and implements all the basic features of a core Spring framework, including Inversion of Control and Dependency Injection.
Show me the code
Platform.sh supports common Java tools, and so we’ll use the Spring Initializr to create a Spring Maven Project with Java 8, and append the Spring Data MongoDB. There’s also a Platform.sh configuration library that reads Platform.sh-specific details, like routes and environmental variables, and exposes them to the application. Here’s the pom.xml file:
Spring Data MongoDB is part of the umbrella Spring Data project, which provides a familiar, consistent, Spring-based programming model for new datastores while retaining store-specific features and capabilities.
We have a User class with three attributes: ID, name, and email. The identifier is primarily designated for MongoDB internal use. The validate fields of the User class in this post will use Bean Validation. Bean Validation—part of JavaEE and JavaSE —ensures that the properties of a Bean meet specific criteria by using annotations.
Spring Data MongoDB focuses on storing data in MongoDB. It also inherits functionality from the Spring Data Commons project, like the ability to derive queries. For the most part, you don’t have to learn the query language of MongoDB; you can write a handful of methods, and the Spring Boot MongoDB handles it for you.
Once Spring uses the MVC approach to building a website, the HTTP requests are handled by a controller. To control these requests, we'll create UserController that will have a @Controller annotation. Each HTTP verb has an annotation, such as GetMapping, thathandles GET HTTP requests, PostMapping that handles POST HTTP requests, and so on.
Thymeleaf parses the HTML templates and evaluates all expressions to render the value that was set in the controller.
The final part of our application is a startup. It’s a standard method that follows the Java convention for an application entry point. SpringApplication bootstraps the app, starting Spring on a Tomcat web server.
Platform.sh structure
The Java application is ready to go! The next step is to set the Platform.sh files required to manage and deploy the application. In our first Java post, we took a deep dive into each detail of these three files:
- One Router (.platform/routes.yaml). Platform.sh allows you to define the routes.
- Zero or more service containers (.platform/services.yaml). Platform.sh allows you to completely define and configure the topology and services you want to use on your project.
- One or more application containers (.platform.app.yaml). You control your application and the way it will be built and deployed on Platform.sh via a single configuration file.
The file that will change on this post is the service file, t allowing you to define a database, search engine, cache, and so on. In this project, we'll set MongoDB instead of MySQL.
Platform.sh and Java
For the database, we need to define where the connection comes from. Platform.sh provides connection credentials to services using the PLATFORM_RELATIONSHIPS environmental variable.
The Java Config library offers classes that will read the information from the variable and return an object that represents the services, like a DataSource for MySQL, PostgreSQL, MongoClient, Redis, or any other services you’ve defined in .platform/services.yaml!
The application is now ready, so it’s time to move it to the cloud with Platform.sh using the following steps:
- Create a new free trial account.
- Sign up with a new user and password, or login using a current GitHub, Bitbucket, or Google account. If you use a third-party login, you’ll be able to set a password for your Platform.sh account later.
- Select the region of the world where your site should live.
- Select the blank template.
After this wizard, Platform.sh will provision the whole infrastructure to you, and Platform.sh will offer your project a remote Git repository. The Platform.sh Git-driven infrastructure means it will automatically manage everything your application needs to push it to the master remote repository. After you set up your SSH keys, you only need to write your code—including a few YAML files that specify your desired infrastructure—then commit it to Git and push.
Code pushed will create both the Java application, a MongoDB instance, and, when it's done, will return an IP address to the service. Let's test the Spring MVC application.
You’ve now seen how a native integration with Spring MVC works, and how only a small change in configuration can easily bring your Maven, Gradle, and Ant managed Java applications to Platform.sh.
In my flagship Java post, you can read more about how you can quickly take your Java apps to the cloud on Platform. sh. And stay tuned for my next post about Elasticsearch versus Solr!