前言
本文是“SpringBoot 1024行代码”系列的第一篇。本文介绍了如何用SpringBoot搭建一个简单的web应用。
准备工作
1 安装jdk1.8
2 安装maven
3 具备Spring和SpringMVC的基础知识
具体步骤
1. 在pom.xml中加入SpringBoot的引用
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 创建一个程序入口类
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);
}
}
3. 添加一个SpringMVC的controller
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@RequestMapping("/hello")
String home() {
return "Hello World!\n";
}
}
4. 验证程序
调用接口
curl 127.0.0.1:8080/hello
返回结果
Hello World!