Spring Boot项目创建

1,581 阅读1分钟
  • Intellij idea创建Spring Boot项目
  1. 选择官方默认的脚手架工具就行
    在这里插入图片描述
  2. next后进入项目信息填写页面,填好信息后直接next就行
    在这里插入图片描述
  3. 在上一步过来后基本上只要确认下代码存储位置就行
    在这里插入图片描述
    到此,一个简易的Spring Boot项目就创建完成了。
  • 新建Spring Boot项目结构简介
    t项目结构
  • 项目启动
  1. 为了方便演示,去pom文件中增加web项目依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 新建controller包,并新建HelloController
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/say")
    public String sayHello() {
        return "first spring boot";
    }
}
  1. 需要将HelloController自动装配到Spring MVC容器中,所以需要再启动类中增加扫描注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
// 扫描配置包,并将此包中符合注解的bean添加到Spring MVC容器中进行管理
@ComponentScan(basePackages = {"com.demo.controller"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
  1. 可在resources目录下配置启动端口,我这里是将application.properties文件改成了application.ymlyml文件格式
server:
  servlet:
    context-path: /demo
  port: 8088
  1. 自定义项目启动banner图:在resources目录下,新建banner.txt,可以在里面配置你喜欢的bannner,我此处配置
${AnsiColor.BRIGHT_YELLOW}
////////////////////////////////////////////////////////////////////
//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//            佛祖保佑       永不宕机     永无BUG                  //
////////////////////////////////////////////////////////////////////
${AnsiColor.BRIGHT_RED}
如需更多banner配置可以去网上搜索下
  1. 项目启动:可以直接执行启动类Application,当然也可以通过idea启动,通过jar、war包方式在后续的文章中会更新说明。
  2. 项目启动成功后,通过在上述的举例代码我们可以在浏览器中输入:http://localhost:8088/demo/hello/say

更多文章: 点击跳转CSDN博客 点击跳转简书博客 公众号:代码小搬运

扫码关注代码小搬运公众号