020_第一个SpringBoot程序


开发环境

  • jdk1.8
  • maven3.6.1

快速搭建的两种方案

Spring官网生成项目,导入IDEA

image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png

IDEA快速创建(集成官网生成项目)

image.png

Package name可以改为:com.qing

image.png
image.png
image.png
image.png
image.png

关键文件

程序的主入口Application.java

image.png

SpringBoot核心配置文件application.properties

image.png

单元测试ApplicationTests.java

image.png

编码测试

创建包,程序主入口同级目录Application.java

只有在主入口同级目录下才会自动扫包

image.png

创建第一个Controller及接口

package com.qing.helloword.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    // 调用:http://localhost:8080/hello
    @RequestMapping("/hello")
    public String hello() {
        return "hello world";
    }
}

运行Application.java

image.png
image.png
image.png

打包 mvn package

image.png
image.png

运行jar包 java -jar ./helloword-0.0.1-SNAPSHOT.jar

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

D:\code\springboot\helloword\target>java -jar ./helloword-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.0)

2021-11-28 16:33:03.539  INFO 8048 --- [           main] com.qing.helloword.HellowordApplication  : Starting HellowordApplication v0.0.1-SNA
PSHOT using Java 1.8.0_271 on L87Y12K91TH8M2R with PID 8048 (D:\code\springboot\helloword\target\helloword-0.0.1-SNAPSHOT.jar started by Adm
inistrator in D:\code\springboot\helloword\target)
2021-11-28 16:33:03.549  INFO 8048 --- [           main] com.qing.helloword.HellowordApplication  : No active profile set, falling back to d
efault profiles: default
2021-11-28 16:33:08.557  INFO 8048 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (h
ttp)
2021-11-28 16:33:08.639  INFO 8048 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-11-28 16:33:08.640  INFO 8048 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/
9.0.55]
2021-11-28 16:33:08.951  INFO 8048 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicat
ionContext
2021-11-28 16:33:08.953  INFO 8048 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initializati
on completed in 5074 ms
2021-11-28 16:33:10.194  INFO 8048 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) w
ith context path ''
2021-11-28 16:33:10.222  INFO 8048 --- [           main] com.qing.helloword.HellowordApplication  : Started HellowordApplication in 8.587 se
conds (JVM running for 13.07)


image.png
image.png

2021-11-28 16:33:10.222  INFO 8048 --- [           main] com.qing.helloword.HellowordApplication  : Started HellowordApplication in 8.587 se
conds (JVM running for 13.07)
2021-11-28 16:34:20.227  INFO 8048 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'd
ispatcherServlet'
2021-11-28 16:34:20.228  INFO 8048 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'

2021-11-28 16:34:20.230  INFO 8048 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

更改端口号

server.port=8081

image.png
image.png
image.png

更改启动图标springboot banner

banner在线生成网站:https://www.bootschool.net/ascii
image.png
image.png
image.png

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.6.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qing</groupId>
    <artifactId>helloword</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloword</name>
    <description>helloword</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>

image.png

posted @   清风(学习-踏实)  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示