Getting Started with Spring Boot: Maven Project Setup and a Simple Web Application
This article introduces Spring Boot, explains its key features, walks through creating a Maven project with a complete pom.xml, adds essential dependencies and plugins, shows how to write a basic @RestController application, and demonstrates various ways to run and hot‑reload the service.
Spring Boot is a recent project of the Spring community that aims to simplify the creation of Spring‑based applications and services, offering a convention‑over‑configuration approach similar to Ruby on Rails.
Key features of Spring Boot include fast start‑up experience, zero‑code generation, no XML configuration, embedded servers, security, metrics, health checks, and easy external configuration.
A simple Maven example is presented. First, create a standard Maven project with a src/main/java directory and a pom.xml file. The pom.xml should contain the following content:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.abel533</groupId>
<artifactId>spring-boot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>If a project already has its own parent POM, you can import Spring Boot's dependency management instead:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>To use Java 8, add a java.version property inside the <properties> section:
<properties>
<java.version>1.8</java.version>
</properties>Add the spring-boot-starter-web starter to enable web functionality, and include the spring-boot-maven-plugin to package the application as an executable JAR. The plugin also supports hot deployment via the springloaded dependency.
Run the project with mvn package to produce an executable JAR, then start it using java -jar xxx.jar. Alternatively, use mvn spring-boot:run for immediate execution.
Create a main application class, for example:
@RestController
@EnableAutoConfiguration
public class Application {
@RequestMapping("/")
String home() {
return "Hello World!";
}
@RequestMapping("/now")
String now() {
return "Current time: " + (new Date()).toLocaleString();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}The @RestController annotation combines @Controller and @ResponseBody, while @EnableAutoConfiguration tells Spring Boot to configure beans automatically based on the classpath. The composite @SpringBootApplication annotation can replace @Configuration, @EnableAutoConfiguration, and @ComponentScan.
To launch the application programmatically, call SpringApplication.run(Application.class, args). Other options include creating a SpringApplication instance or using SpringApplicationBuilder for more complex setups.
When the application starts, Spring Boot initializes an embedded Tomcat server on port 8080, registers the request mappings, and logs the startup sequence, as shown in the sample log output.
For further details, consult the official Spring Boot documentation at http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/index.html .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
