用Maven创建SpringBoot程序
用Maven创建SpringBoot程序
-
打开idea,创建普通的Maven工程
-
修改pom.xml
注意添加
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.example
springboot-01-maven
1.0-SNAPSHOT
spring-boot-starter-parent
org.springframework.boot
2.6.3
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
8
8
- 在com.ajream包下,添加类:
HelloWorldApplication
package com.ajream;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
到此就可以启动该springboot程序了,主程序入口就是从
HelloWorldApplication中的main函数进入,启动后可以打开浏览器输入http://localhost:8080(默认使用8080端口)查看程序运行结果
如果8080端口被占用,可以在resource下创建配置文件
application.properties,按如下方式修改端口号:server.port=8082
- 在com.ajream.controller包下,添加类HelloController
这一步不是必须的,完成上面3步就可以启动springboot程序了
package com.ajream.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String index(){
return "Hello! Welcome to SpringBoot!!!";
}
}
访问 http://localhost:8082/hello 查看运行结果

