3、SpringBoot:helloworld

引用文章:微信公众号狂神说

创建helloworld项目

准备工作

我们将学习如何快速的创建一个Spring Boot应用,并且实现一个简单的Http请求处理。通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。

我的环境准备:

  • java version "1.8.0_181"

  • Maven-3.6.1

  • SpringBoot 2.x 最新版

开发工具:

  • IDEA

使用Spring Initializr页面创建项目

创建基础项目

Spring官方提供了非常方便的工具

Spring Initializr:https://start.spring.io/ 来帮助我们创建Spring Boot应用。

【目标一:使用Spring Initializr页面创建项目

步骤:

  1. 打开 https://start.spring.io/

  2. 填写项目信息

  3. 点击”Generate Project“按钮生成项目;下载此项目

  1. 解压项目包,并用编译器以Maven项目导入,以IntelliJ IDEA为例:

  2. 导入这个Maven项目,一路下一步即可,直到项目导入完毕。如果是第一次使用,可能速度

    会比较慢,需要耐心等待一切就绪

 

项目结构分析

通过上面步骤完成了基础项目的创建。就会自动生成以下文件。

  • 程序的主程序类 DemoApplication

  • package com.zxh.helloworld;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    // @SpringBootApplication本身就是一个spring的一个组件
    
    // 程序的主入口
    @SpringBootApplication
    public class HelloworldApplication {
    
        //SpringApplication应用的
        public static void main(String[] args) {
            SpringApplication.run(HelloworldApplication.class, args);
        }
    
    }
  • 一个 application.properties 配置文件

    # SpringBoot 核心配置文件
  • 一个测试类

  • package com.zxh.helloworld;
    ​
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    ​
    @SpringBootTest
    class HelloworldApplicationTests {
    ​
        @Test
        void contextLoads() {
        }
    ​
    }

生成的DemoApplication和测试包下的DemoApplicationTests类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。

pom.xml 分析

打开pom.xml,看看Spring Boot项目的依赖:

<?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>
    <!-- 有一个父项目,所以集成了父项目的所有依赖
         <relativePath/>标签表示这是一个远程,在线的
    -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <!-- 坐标:groupId和artifactId
         groupId:公司域名
         artifactId:项目名称
    -->
    <groupId>com.zxh</groupId>
    <artifactId>helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloworld</name>
    <description>Demo project for Spring Boot</description><properties>
        <java.version>1.8</java.version>
    </properties><!-- spring-boot-starter所有的springboot依赖都是用这个开头,导入时直接在后面加-->
    <dependencies>
        <!-- web依赖: 集成了tomcat,自动装配(xml配置等)-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency><!-- 单元测试,和junit一样-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies><build>
        <!-- 打jar包的插件-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build></project>
 

如上所示,主要有四个部分:

  • 项目元数据信息:创建时候输入的Project Metadata部分,也就是Maven项目的基本元素,包括:groupId、artifactId、version、name、description等

  • parent:继承spring-boot-starter-parent的依赖管理,控制版本与打包等内容

  • dependencies:项目具体依赖,这里包含了spring-boot-starter-web用于实现HTTP接口(该依赖中包含了Spring MVC),官网对它的描述是:使用Spring MVC构建Web(包括RESTful)应用程序的入门者,使用Tomcat作为默认嵌入式容器。;spring-boot-starter-test用于编写单元测试的依赖包。更多功能模块的使用我们将在后面逐步展开。

  • build:构建配置部分。默认使用了spring-boot-maven-plugin,配合spring-boot-starter-parent就可以把Spring Boot应用打包成JAR来直接运行。

编写HTTP接口

  1. 在主程序的同级目录下,新建一个controller包【一定要在同级目录下,否则识别不到】 

  2. 在包中新建一个Controller类

    package com.kuang.springboot.controller;
    ​
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    ​
    @RestController
    public class HelloController {
    ​
        @RequestMapping("/hello")
        public String hello() {
            return "Hello World";
        }
        
    }
  3. 编写完毕后,从主程序启动项目,浏览器发起请求,看页面返回;

    • 控制台输出了SpringBoot 的 banner

    • 控制条输出了 Tomcat 访问的端口号!

    • 访问 hello 请求,字符串成功返回! 

小结

  • 简单几步,就完成了一个web接口的开发,SpringBoot就是这么简单。所以我们常用它来建立我们的微服务项目!

使用IDEA快速构建项目

之前,我们在官网上直接快速构建了一个springboot项目,IDEA也可以做到,我们来看下具体步骤:

创建项目,运行

  1. 创建一个新项目

  2. 选择spring initalizr , 可以看到默认就是去官网的快速构建工具那里实现

  3. 填写项目信息

  4. 选择初始化的组件,可以不用任何添加直接ok,之后手动导入依赖;

  5. 填写项目路径

  6. 等待项目构建成功

  7. 我们在SpringBootApplication的同路径下,建一个包 controller ,建立一个类HelloSpringBoot

  8. 编写代码

    //@RestController 的意思就是 Controller 里面的方法都以 json 格式输出
    @RestController
    public class HelloSpringBoot {
    ​
        @RequestMapping("/hello")
        public String hello(){
            return "Hello,SpringBoot!";
        }
    }
  9. 启动主程序,打开浏览器访问 http://localhost:8080/hello,就可以看到效果了!

详细信息

配置文件

# 修改tomcat端口号
server.port=8081

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.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zxh</groupId>
    <artifactId>springboot-01-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-01-helloworld</name>
    <description>OneDemo project for Spring Boot</description>

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

    <dependencies>
        <!-- 启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- 因为版本会被父依赖管理,所以不需要声明版本号-->
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>
 

我们在之后的学习中,直接使用IDEA创建项目即可,方便快捷!这么简单的东西背后一定有故事,我们之后去进行一波源码分析!

将项目打成jar包

  1. 首先需要打jar包的插件

  2. 点击右边的生命周期(lifecycle)里的package命令,进行打包

  3. 控制台看到 BUILD SUCCESS,打包成功;

  4. 打成了jar包后,就可以在任何地方运行了!OK

  5. 比如可以用cmd运行,输入java -jar jar包的名称运行;就可以不用idea运行

    <!-- 1、进入jar包所在目录 -->
    cd /d F:IDEA-workspacespringboothelloworld	arget
    <!-- 2、利用java命令语句,运行jar包 -->
    java -jar helloworld-0.0.1-SNAPSHOT.jar

  6. 运行成功,通过浏览器就可以直接访问接口

彩蛋

  1. 如何更改启动时显示的字符拼成的字母,SpringBoot呢?

  2. https://www.bootschool.net/ascii-art/comic?pageNO=4 ,直接输入要生成的字母,系统会自动转换,然后复制下面转换好的字符;

  3. 到项目下的 resources 目录下新建一个txt文件就可以,banner.txt

 ///                            /                     
|   / |                         /                       
|  /  |                       /                         
|  /  |----------------------|     /     |              
| /   |                      |    /      |              
|/    |                      |   /       |              
|    /|                      |  | (  ) |  |              
|   / |                      |  | (  ) |  |              
|  /  |                 /   |  |      |  |   /         
|  /  |                /    |  |      |  |  /          
| /   |               |----| |  |      |  | |----|       
|/    |---------------|    | | /|   .  | | |    |       
|    /|               |    | /  |   .  |   |    |       
|   / |               |    /    |   .  |        |       
|  /  |               |  /      |   .  |        |       
|  /  |---------------|/        |   .  |        |       
| /   |              /   NASA   |   .  |  NASA          
|/    |              (          |      |           )     
|///|               |    | |--|      |--| |    |       
------------------------/  -----/  /  -----/  --------
                        \//     \//\//     \//        
                         /       /  /       /      


                   _____
/` `
/' `\_____
_ / ___/_
/` ` /__/` `
/ `\__ /' `\_____
_ / ` / ___/_
/` ` /_____/` /__/` `
/' `\_____ /' `\_____
_ / /_ _\___ / ___/_
/` ` /_____/ `./' ` /` `
/' `\_____ `\_____ `\_____ `\_____
/ / / / / / / /
` /_____/' /_____/' /_____/' /_____/'

zxh

然后重启试试吧!

几个常用的字符画生成网站


下面介绍如何自定义为gif动图:

打开网址:https://giphy.com/ 搜索 ascii

点击任意gif进行下载

然后将下载下来的 .gif文件复制到resources/目录下改名名为banner.gif即可

注意,如此操作在ide中无法实现动图动态展示,我们需要在命令行中启动项目,两种方式都可以:

1.切换到项目目录中 mvn springboot:run

2.切换到项目目录中 mvn install

 切换到 target目录 java -jar 打包好的项

 

致力于记录学习过程中的笔记,希望大家有所帮助(*^▽^*)!
原文地址:https://www.cnblogs.com/zxhbk/p/12683991.html