首先创建一个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方法的路径:
第一个例子完成。