Develop your first Spring Boot application

Develop your first Spring Boot application

Let's develop a simple "Hello World!" web application in Java to highlight some key features of Spring Boot. We will use Maven to build the project since most IDEs support it.

Note : The spring.io website contains a number of "getting started" guides for using Spring Boot. If you're looking for a solution to a specific problem, check it out first.

Before starting, you need to open a terminal and check if the available version of Java and Maven are installed:

$ java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
$ mvn -v
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00)
Maven home: /Users/user/tools/apache-maven-3.1.1
Java version: 1.7.0_51, vendor: Oracle Corporation

Note : This example requires you to create your own folder. What follows assumes that you have created a suitable folder and that it is your "current directory".

Create POM

We need to start by creating a Maven pom.xml file. The pom.xml is the recipe used to build the project. Open your favorite text editor, and add the following:

<?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.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Additional lines to be added here... -->

    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>

This will give you a working build that you can mvn packagetest by running (for now you can ignore the "jar will be empty - contains nothing!" warning).

Note : Currently you can import the project into an IDE (most modern Java IDEs include built-in support for Maven). For simplicity, we will continue to use a normal text editor for this example.

Add classpath dependency

Spring Boot provides many "Starter POMs", which allow you to easily add jars to your classpath. Our sample program has been used in the partent node of the POM spring-boot-starter-parent. spring-boot-starter-parentis a special starter that provides useful Maven defaults. Also, it provides a dependency-managementnode so that you can omit the version tag for "blessed" dependencies.

Other "Starter POMs" simply provide dependencies that you may need to develop certain types of applications. Since we're developing a web application, we'll add a spring-boot-starter-webdependency - but before that, let's see what we have so far:

$ mvn dependency:tree
[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT

mvn dependency:treecommand to print your project dependencies in a tree representation. You can see that spring-boot-starter-parentthere are no dependencies provided by itself. Edit our pom.xml and add spring-boot-starter-webdependencies under the parent node:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

If you run it again mvn dependency:tree, you'll see that you now have some other dependencies, including the Tomcat web server and Spring Boot itself.

Guess you like

Origin blog.csdn.net/2301_76484015/article/details/130485596