项目中引入依赖
<!-- 引入了父Maven项目 ,继承父Maven项目所有的配置信息
spring-boot-starter-parent 又引入一个父Maven项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
spring-boot-dependencies 帮我们管理了SpringBoot应用中所有的依赖的版本,
以后我们导入已有依赖就不需要写版本号了,它帮我们解决第三方库直接的版本冲突问题
名次:SpringBoot的版本仲裁中心
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<!--
starter 场景启动器:不同的场景启动器维护了所对应的所有依赖,从而简化maven文件书写
spring-boot-starter-web:使用Spring MVC构建Web(包括RESTful)应用程序。
使用Tomcat作为默认的嵌入式容器
-->
<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>
建包并创建控制器
@RestController // @Controller +@ResponseBody
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/world")
public String sayHi(){
return "hello world!";
}
}
编写启动类
@SpringBootApplication // 标记成Springboot的启动类
public class Application {
public static void main(String[] args) throws IOException {
SpringApplication.run(SpringApplication.class,args);
}
}
修改端口
如果出现
Web
server
failed
to
start.
Port
0
was
already
in
use.
说明端口正在使用,我们需要修改默认端口
server.port=8088
部署服务器
<!--部署springboot的插件, 只有加了这个插件 当运行 java -jar xxxx.jar 才能正常启动-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
将这个应用打成jar包,直接使用java -jar的命令进行执行;