What is Spring boot? , what are the benefits of using spring boot? These things not too much statement, self-Baidu, in this as just a developer's point of view, record their own learning spring boot process, as a learning note to share with you, if there is anything not rigorous or wrong place, please leave a message to point out. First, initialize the structure of the project
Create a basic MAVEN structure project framework through the official website (http://start.spring.io/), such as! Click the button "Generation Project" to generate a basic directory, and then import to eclipse, you can write code! In the process of importing the project, select Maven-Existing maven Projects.
The overall project structure is as follows:
Ii. Introduction of Web Modules
1. In the MAVEN siege configuration file "Pom.xml", add
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
There are two modules in the Pom.xml file by default:
Spring-boot-starter: core modules, including automatic configuration support, logging and YAML;
Spring-boot-starter-test: Test modules, including JUnit, Hamcrest, Mockito
2, write controller, in the package "controller" directory to create "Helloworldcontroller.java" class, through the class annotations, control the specific format of the returned data:
Package Com.zh.SpringBootDemo.controller;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.ModelMap;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;
@RestController //returns JSON data
@Controller //Jump to the corresponding page
Public class Helloworldcontroller {
@RequestMapping("/hello")
Public String Index () {
return "Hello World";
}
@RequestMapping ("/")
Public String Index (Modelmap map) {
//Add an attribute to read in the template
Map.addattribute ("host", "http://blog.didispace.com");
//Return template file name, corresponding to src/main/resources/templates/index.html
return "index";
// }
}
Third, run view
After running "Springbootdemoapplication.java" under the project root, open "http./Localhost:8080/hello" To see the returned results!
Spring Boot Learning Advanced Notes (i)-initial experience, creating basic Web Features