1. Open IDEA, create a new project, and select Spring Initializr
2. Enter Artifact
3. Check Web
4. Click finish
5. Enter the project and delete the following content
pom.xml file:
<?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>springbootdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springbootdemo</name> <description>Demo project for Spring Boot</description> <!--Start dependencies--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--Develop web project-related dependencies--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--springboot unit test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--maven-build--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
6. Create a HelloController
package com.example; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "hello,this is a springboot demo"; } }7. The SpringbootdemoApplication automatically generated by the program will have an annotation of @SpringBootApplication. This annotation is used to indicate that this class is the entry of the program.
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //Entry@SpringBootApplication public class SpringbootdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); } }@SpringBootApplication enables Spring's component scanning and springboot's automatic configuration functions, which is equivalent to combining the following three annotations together
(1) @Configuration: The table name This class uses Java-based configuration, and uses this class as a configuration class
(2) @ComponentScan: Enable annotation scanning
(3) @EnableAutoConfiguration: Turn on the automatic configuration function of springboot
8. Run SpringbootdemoApplication class
test:
Enter http://localhost:8080/hello in the address bar
9. Start using the method of starting jar package
(1) First enter the directory where the project is located. If it is a mac system, right-click on the project, select Reveal in Finder, and right-click on the Windows system and select Show in Explorer on the project, you can open the directory where the project is located.
(2) Open the terminal and enter the directory where the project is located
cd /Users/shanml/IdeaProjects/SpringbootDemo
Enter mvn install to build the project
(3) After the construction is successful, there will be an additional jar package in the project target folder.
(4) Use java -jar springbootdemo-0.0.1-SNAPSHOT.jar
Just start the jar package
refer to:
Senior Brother Liao: Learn Springboot in two hours
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.