首先創建一個maven項目
maven項目創建完成之后,找到pom.xml配置節點。需要springboot-starter-web ,springboot-starter-test,springboot-starter-parent,springboot-starter-plugin。
pom.xml配置文件:
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.happy</groupId> <artifactId>11SpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>11SpringBoot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
這樣環境搭建完成。
做第一個例子,首先創建一個controller:
controller代碼:
package cn.happy; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String ob(){ System.out.println("我們進入了App"); return "success"; } }
之后在App類中加上一個注解:SpringbootApplication
在App類的mian方法中調用 : SpringApplication.run(App.class,args);
package cn.happy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class,args); } }
然后啟動mian方法:
打開百度輸入controller方法的路徑:
第一個例子完成。