#maven创建SpringBoot需要导入
<!-- 指定Spring Boot的版本 2.0.4.RELEASE 1.5.9-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<!-- 导入Spirng Boot web 所需的jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 导入Spirng Boot 的单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 可以将应用打成一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
#在src/java目录创建一个类名为HelloWorldTest的类
package com.xurujie;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldTest {
public static void main(String[] args) {
SpringApplication.run(HelloWorldTest.class,args);
}
}
#创建一个HelloController的类
package com.xurujie.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
由于springboot中自带了tomcat,所以直接运行就好
点击运行HelloWorldTest的中的main方法可以启动tomcat服务器
启动的tomcat服务器默认端口为8080(可以改)
访问localhost:8080/hello
即可看到浏览器输出Hello World.