springboot-内置Tomcat
Spring Boot使用内嵌的Tomcat作为服务器,可以直接打包应用为jar文件,然后运行这个jar文件来启动服务。
以下是一个简单的Spring Boot应用的例子:
- 创建Spring Boot项目:
$ curl -s https://start.spring.io/starter.tgz \
| tar -xzvf - \
| mkdir demo \
| cd demo \
| tar -xzvf -
- 添加依赖到
pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 创建一个简单的Controller:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
- 创建应用的启动类:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 打包应用为jar:
$ mvn clean package
- 运行应用:
$ java -jar target/demo-0.0.1-SNAPSHOT.jar
应用启动后,你可以通过访问http://localhost:8080/hello
来测试你的应用。
评论已关闭