1. 创建项目

1、 项目类型:Spring Initializr,选择Default:https://start.spring.io

【Spring Boot】1. Idea创建Spring Boot项目_spring boot

2、输入项目信息,如下图所示:

【Spring Boot】1. Idea创建Spring Boot项目_java_02

3、选择项目信息

【Spring Boot】1. Idea创建Spring Boot项目_java_03

4、输入项目的保存目录等信息,点击【Finish】即可

【Spring Boot】1. Idea创建Spring Boot项目_# Java开发笔记_04

2. 第一个Hello项目

1、首先确定pom.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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ecxhit</groupId>
    <artifactId>hello-spring-boot-2</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>hello-spring-boot-2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、Application中写入Hello代码:

【Spring Boot】1. Idea创建Spring Boot项目_spring boot_05

代码如下:

package com.ecxhit.hellospringboot2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class HelloSpringBoot2Application {


    @RequestMapping(value = "",method = RequestMethod.GET)
    public String sayHi(){
        return "Hello Spring Boot";
    }

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

}

3、右击,运行项目(Ctrl+Shitf+F10)

【Spring Boot】1. Idea创建Spring Boot项目_java_06

【Spring Boot】1. Idea创建Spring Boot项目_前后端全栈开发_07

4、浏览器输入地址,即可访问

【Spring Boot】1. Idea创建Spring Boot项目_spring boot_08

3、自定义Banner(佛祖保佑)

【Spring Boot】1. Idea创建Spring Boot项目_java_09

示例:

                          _ooOoo_
                         o8888888o
                         88" . "88
                         (| ^_^ |)
                         O\  =  /O
                      ____/`---'\____
                    .'  \\|     |//  `.
                   /  \\|||  :  |||//  \
                  /  _||||| -:- |||||-  \
                  |   | \\\  -  /// |   |
                  | \_|  ''\---/''  |   |
                  \  .-\__  `-`  ___/-. /
                ___`. .'  /--.--\  `. . ___
              ."" '<  `.___\_<|>_/___.'  >'"".
            | | :  `- \`.;`\ _ /`;.`/ - ` : | |
            \  \ `-.   \_ __\ /__ _/   .-` /  /
      ========`-.____`-.___\_____/___.-`____.-'========
                           `=---='
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
              佛祖保佑       永不宕机      永无BUG

如果需要自定义字符,可以百度

4、单元测试

test/java/下的文件代码如下:

【Spring Boot】1. Idea创建Spring Boot项目_spring boot_10

源码如下:

package com.ecxhit.hellospringboot2;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URL;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloSpringBoot2Application.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloSpringBoot2ApplicationTests {

    @LocalServerPort
    private int port;

    private URL base;

    @Autowired
    private TestRestTemplate template;

    @Before
    public void setUp() throws Exception{
        this.base = new URL("http://localhost:"+port+"/");
    }


    @Test
    public void contextLoads() {
        ResponseEntity<String> responseEntity = template.getForEntity(base.toString(),String.class);
        assertThat(responseEntity.getBody(),equalTo("Hello Spring Boot"));

    }

}

至此。