快速创建项目
Maven 项目设置
- 在 opm.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 所有springboot都集成自它 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
</parent>
<!-- web 场景启动器 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
code
- 编写根 code
package com.boot.controller;
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";
}
}
- 编写 controller
package com.boot.controller;
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";
}
}
jar 包
我们可以创建一个可执行的 jar 包
- 在 opm.xml 添加配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
运行 jar 包的流程
1.执行 maven 命令
mvn clean package2.找到当前根目录文件路径 3.使用命令运行
java -jar xxxx.jar配置 jar 包外部化配置
在 jar 包同级目录下新建 application.properties 文件
server.port=8888
重新运行java -jar xxxx.jar后就可以在 8888 端口上打开本服务