Build your web app in 20 minutes

Recently needed to build a Spring Rest application quickly and decided to try Spring Boot and the H2 database.
If you haven’t heard about Spring Boot, it is a framework designed to simplify the bootstrapping and development of a new Spring application, making easy to create stand-alone, production-grade Spring based applications that you can “just run”. The framework takes an opinionated approach to configuration, freeing developers from the need to define boilerplate configuration. In that, Spring Boot aims to be a front-runner in the ever-expanding rapid application development space. These are the main features of this framework:
- Create stand-alone Spring applications.
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files).
- Provide opinionated ‘starter’ POMs to simplify your Maven configuration.
- Automatically configure Spring whenever possible.
- Provide production-ready features such as metrics, health checks and externalized configuration.
- Absolutely no code generation and no requirement for XML configuration.
H2 is an open source, embedded database built on java. You can easily embed H2 database into your application. It has a built in web based console that you can use to interact with the database. You can use H2 database as an in-memory database, embedded database or network database. This example shows how to use it as an in-memory database.
Now that we introduce the main technologies of the project, let's start with the fun part. It is my understanding that there are two easy ways to make a project from greenland. The first one is using the Spring Initializer tool, a web application that can generate a Spring Boot project structure for you. The second option is to create a new Maven project using Eclipse, Spring Tool Suite or any other IDE you are familiar with. In my case I use Eclipse. This is the pom.xml:
As you can see the pom.xml configuration is dead simple, it just works. With those few lines you have the jars to develop a REST Spring application, use the H2 in-memory database, have Hibernate as your ORM and Tomcat as your application container.
I’m sure your are asking to yourself, ok… How can I start my fancy new application. You will need something like this:
@SpringBootApplication is a convenience annotation that adds all of the following:
@Configurationtags the class as a source of bean definitions for the application context.@EnableAutoConfigurationtells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.- Normally you would add
@EnableWebMvcfor a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up aDispatcherServlet. @ComponentScantells Spring to look for other components, configurations, and services in thehellopackage, allowing it to find the controllers.
The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
Finally the database needs a configuration file, application.properties :
# H2spring.h2.console.enabled=truespring.h2.console.path=/h2 #Datasourcespring.datasource.url=jdbc:h2:file:~/testspring.datasource.username=saspring.datasource.password=spring.datasource.driver-class-name=org.h2.Driver
When the application is up and running the database console can be accessible on http://localhost:8080/h2/
This is all you need to start developing, in 20 minutes or less you have all you need to implement the web app of your dreams. If you are interested in check the rest of the code you can find it here:
Via: Spring Boot and Spring Framework
