创建一个普通maven项目
不需要勾选Create from archetype
改造成SpringBoot项目
1. pom.xml的project标签加入父依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/>
</parent>
2. 加入web依赖包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3. 新建启动类
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationStartup
{
public static void main(String[] args) {
SpringApplication.run(ApplicationStartup.class, args);
}
}
4. 新建WEB测试接口
package com.app.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/TestClass")
public class Test {
@GetMapping("TestFunction")
@ResponseBody
public String onMethod() {
return "return some json";
}
}
5.输入url观察结果
http://localhost:8080/TestClass/TestFunction
6. 打包运行
pom.xml的project标签中加入插件,用来运行指定的启动类com.app.ApplicationStartup的main方法
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.app.ApplicationStartup</mainClass>
</configuration>
</plugin>
</plugins>
</build>
依次执行maven命令 clean 清空之前运行生成的target目录 package 打jar包 在target目录找到生成的jar包 把jar包拷贝到其他地方进行cmd运行
java -jar boot_001-1.0-SNAPSHOT.jar
|