Use Spring Boot to develop "Hello World" Web applications, springhello
Environment preparation
Because many ides currently support Maven, we will use Maven to build this project;
Before you start, install Java and Maven:
This project will be developed based on Spring Boot 1.4.3.RELEASE. The recommended Java version is Java 7 + and the maven version is 3.2 +. You can run the following command to check whether the project meets the requirements:
Create POM
Because Maven is used for building, we need to createpom.xml
File, used to build this project, open your favorite editor (such as Notepad ++), and add 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.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.4.3.RELEASE</version> </parent> <!-- Additional lines to be added here... --></project>
Add dependency
Spring Boot provides a series of "starters" to make it easy to add dependencies;
Because this project is a Web project, we choose to addspring-boot-starter-web
"Starter" dependency;
Addspring-boot-starter-web
Dependency
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
For more intuitive visibilityspring-boot-starter-web
For the introduced dependency, we can use the mvn dependency: tree Command to view the dependency tree of the project. The following is the difference before and after adding:
(Note that although it inherits spring-boot-starter-parent, it does not introduce any dependencies ):
After:
We can find that the project has no dependencies before it is added.spring-boot-starter-web
Dependencies such as Tomcat, Jackson, and Spring Boot are automatically added.
Finally, it is worth noting that for simplicity, our project inherits the spring-boot-starter-parent Project, spring-boot-starter-parent pre-defines many plug-in management configurations, compilation levels (Java 6), encoding (UTF-8), and so on, but does not add any dependencies, if you are interested, you can view the pom of the spring-boot-starter-parent Project. the link to the xml file is as follows:
Https://github.com/spring-projects/spring-boot/blob/master/spring-boot-starters/spring-boot-starter-parent/pom.xml
Write code
To complete this application, we need to create a simple Java file, as shown below. By default, Maven will gosrc/main/java
Directory to compile the java file, so we need to first create this directory structure, and then addsrc/main/java/Example.java
File:
import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController@EnableAutoConfigurationpublic class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); }}
Observe the above code and we use@ RestController and@ RequestMapping two Spring MVC
Annotation,
@RestController
Indicates that the class is a Controller and the returned content is directly written into the response body;
@RequestMapping
Used for url route ing. Here, the Http request with the surface path "/" will be mapped to home () for processing;
In addition@EnableAutoConfiguration
Annotation. After this annotation is added, Spring Boot will automatically configure Spring Based on the dependencies you add.spring-boot-starter-web
Tomcat and Spring MVC are added. Therefore, the automatic Spring Boot configuration assumes that you are developing a web application and add relevant Spring configurations accordingly.
Finally, we should pay attention to the main method, which is no different from the common main method. There is only one sentence. Call the SpringApplication run method to start the application.
Run the application using maven commands
Because it inheritsspring-boot-starter-parent
Project, so we can directly usemvn spring-boot:run
CommandTo run the program, as shown below:
$ mvn spring-boot:run . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.3.RELEASE)....... . . ........ . . . (log output here)....... . . ......... Started Example in 2.222 seconds (JVM running for 6.514)
After it is started, enter http: // localhost: 8080/in the browser to view the result returned by the home method in Controller:
In additionctrl-c,
You can disable the application.
Create an executable jar package
Simple: Add a maven plug-in with the following code;
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>
Then runmvn package
Command (note,The POM of spring-boot-starter-parent has already been configured for us and bound to the repackage goal. If the inherited parent POM is not used, we need to do more configuration. For details, refer to plugin documentation.
):
After packaging, we can find the helloworld-0.0.1-SNAPSHOT.jar package under the target directory.
If you want to run the program, it's easy to enter the java-jar target/helloworld-0.0.1-SNAPSHOT.jar command to start the application, as shown below:
$ java -jar target/helloworld-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.3.RELEASE)....... . . ........ . . . (log output here)....... . . ......... Started Example in 2.536 seconds (JVM running for 2.864)
Similarly, enter http: // localhost: 8080/in the browser to view the result returned by the home method in the Controller.
IDE recommendations
Finally, although this example does not use any IDE, it is mainly because it is relatively simple; if developing complex applications, it is strongly recommended to use IDE, such as Spring Tool Suite, I am using the sts-3.8.3.RELEASE version.
References
Http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/htmlsingle/