最基础的项目构建方法:
- 此方式【新建项目】区别于 通过
生成器
“构建项目”。 - 这只是建一个简单的项目。
构建Spring Boot项目
方式1:创建maven工程
创建一个maven工程(建议:java工程,也可以是web工程),无需勾选maven骨架。
方式2:创建spring initializr工程
(1)通过idea工具创建工程时,不再选择maven了而是选择spring initializr。然后去勾选相关依赖。
如果默认服务器URL不行可以换为
https://start.aliyun.com/
项目IDEA中呈现:
如果有自己配置的maven仓库的话,就要在setting里面设置,点击file - setting- 直接搜索maven,就可以看到。在这里可以配置自己的maven版本和仓库。
(2)添加到maven,重新加载项目;
(3)执行HelloworldApplication
程序;
到这里,idea的开发环境已经搭建好。
(4)编写HelloController
- 在工程的src目录下创建
package com.lzl.helloworld.helloController
文件夹
注:【helloController】文件夹与HelloworldApplication
文件在一级目录。 - 创建
HelloController
类
HelloController
文件:
package com.lzl.helloworld.helloController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
/**
* 请求 /hello 输出hello springboot!
* @return
*/
@GetMapping(value = "/hello")
public String hello(){
return "hello World!";
}
// @GetMapping("/hello")
// public String hello(@RequestParam(value = "name", defaultValue = "Worldddddddd") String name) {
// return String.format("Hello %s!", name);
// }
}
HelloworldApplication
文件:
package com.lzl.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloworldApplication {
public static void main(String[] args) {
// 只需要run一下,就能发布一个springboot应用
// 相当于之前将web工程发布到tomcat服务器,只是在springboot中集成了tomcat插件
SpringApplication.run(HelloworldApplication.class, args);
}
}
(5)启动项目。
(6)在浏览器中打开 http://localhost:8080/hello
官方文档: https://spring.io/quickstart
SpringBoot学习之《深入浅出SpringBoot》