0%

初识SpringBoot--HelloWorld

通过官网生成文档创建项目(基于IDEA-U)

文档生成

  • 登录官网 spring.io
  • 选择导航栏上的 PROJECTS
  • 选择 SPRING BOOT
  • Learn 栏里,我们可以看到当前的几个版本,可以选择第一个最新的稳定版本,当前是 2.2.3
  • 回到 Overview,滑倒最下方,可以看到 Quick start
    • 选择 Spring Initializr
    • 进入 Spring Initializr-Bootstrap your application 页面
  • 进行元数据配置
    • Project Metadata:
      • Group –练手项目就是包名前缀
      • Artifact –同理
      • Options –可以选择 JDK 版本
    • Dependencies:
      • 输入 Web,添加 Spring Web
  • 生成文件
    • 最下方选择 Generate
  • 下载完成后解压到相关文件夹完毕

导入项目

  • 打开 Intellij IDEA
  • 选择 导入项目(import project)
    • 到刚刚的文件夹选择就行,比如 helloworld
  • 相关配置一选就好了

通过 Intellij IDEA 创建项目(基于IDEA-U)

  • IDEA 中选择 新建项目

  • 选择 Spring Initializr

    • 默认 Default URL
    • 可以看到这是官网的路径
  • 下一步,元数据配置,和官网下载的信息一样

  • 下一步,选择依赖 —— Spring Web

  • 下一步,改一下项目名称就OK了


(基于IDEA-C)社区版

  • 打开IDEA,在设置中找到插件 Plugins
  • 搜索 Spring Assistant,选择安装 Install
  • 接下来暂不介绍

第一个Hello, world! 项目

HelloworldApplication

  • 是程序的主入口
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package cn.nopech.helloworld;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    // 程序的主入口
    @SpringBootApplication
    public class HelloworldApplication {

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

    }

    cn.nopech.helloworld 下新建包 controller

  • 新建java程序 HelloConroller
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    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!";
    }
    }

附上项目结构图

启动程序

成功了可以看到如下结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.3.RELEASE)

2020-01-19 12:09:47.686 INFO 12376 --- [ main] c.n.helloworld.HelloworldApplication : Starting HelloworldApplication on LAPTOP-83O6E23J with PID 12376 (D:\Code\SpringBoot\helloworld\target\classes started by 12068 in D:\Code\SpringBoot\helloworld)
2020-01-19 12:09:47.688 INFO 12376 --- [ main] c.n.helloworld.HelloworldApplication : No active profile set, falling back to default profiles: default
2020-01-19 12:09:48.218 INFO 12376 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-01-19 12:09:48.233 INFO 12376 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-01-19 12:09:48.233 INFO 12376 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-01-19 12:09:48.280 INFO 12376 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-01-19 12:09:48.280 INFO 12376 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 561 ms
2020-01-19 12:09:48.402 INFO 12376 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-19 12:09:48.505 INFO 12376 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-19 12:09:48.507 INFO 12376 --- [ main] c.n.helloworld.HelloworldApplication : Started HelloworldApplication in 1.044 seconds (JVM running for 1.588)
2020-01-19 12:09:48.641 INFO 12376 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-19 12:09:48.641 INFO 12376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-01-19 12:09:48.645 INFO 12376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
  • Tomcat started on port(s): 8080 (http) with context path ''
    • 这告诉我们端口号为8080,可以在本地访问

到浏览器 localhost:8080,可以看到下图结果

  • 跳转到 localhost:8080/hello
    • 可以看到语句 Hello, world!
  • 成功,完毕

相关配置

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.nopech</groupId>
<artifactId>helloworld</artifactId>
<version>2.2.3-SNAPSHOT</version>
<name>helloworld</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>11</java.version>
</properties>


<dependencies>

<!-- web依赖:tomcat, disapatcharServlet, xml.. -->
<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>

<!-- 打jar包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

resources/application.properties

  • 这是 SpringBoot 核心配置文件

    更改端口号

    1
    2
    3
    4
    # application.properties
    # SpringBoot 核心配置文件
    # 更改项目端口号
    server.port=8088

    更改 banner

  • resources 下新建文件 banner.txt,将自己想要的banner贴进去

附上修改完端口号和banner的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
                                                         /*[*/#include<stdio.h>//                   
#include<stdlib.h>//]++++[->++[->+>++++<<]<][(c)2013]
#ifndef e//[o
#include<string.h>//]![misaka.c,size=3808,crc=d0ec3b36][
#define e 0x1//
typedef struct{int d,b,o,P;char*q,*p;}f;int p,q,d,b,_=0//|
#include __FILE__//]>>>[->+>++<<]<[-<<+>>>++<]>>+MISAKA*IMOUTO
#undef e//[->[-<<+<+<+>>>>]<<<<<++[->>+>>>+<<<<<]>+>+++>+++[>]]b
#define e(c)/**/if((_!=__LINE__?(_=__LINE__):0)){c;}//[20002,+[-.+]
,O,i=0,Q=sizeof(f);static f*P;static FILE*t;static const char*o[]={//
"\n\40\"8oCan\40not\40open %s\n\0aaFbfeccdeaEbgecbbcda6bcedd#e(bbed$bbd",
"a6bgcdbbccd#ead$c%bcdea7bccde*b$eebbdda9bsdbeccdbbecdcbbcceed#eaa&bae$cbe",
"e&cbdd$eldbdeedbbdede)bdcdea&bbde1bedbbcc&b#ccdee&bdcdea'bbcd)e'bad(bae&bccd",
"e&bbda1bdcdee$bbce#b$c&bdedcd%ecdca4bhcdeebbcd#e$b#ecdcc$bccda7bbcc#e#d%c*bbda",
">bad/bbda"};static int S(){return(o[p][q]);}static/**/int/**/Z=0 ;void/**/z(int//
l){if(/**/Z-l){Z=l;q++;if(p<b*5&&!S()){p+=b;q=0;}}}int main(int I, /**/char**l){//
d=sizeof(f*);if(1<(O=_)){b=((sizeof(o)/sizeof(char*))-1)/4;q=22; p= 0;while(p<b*5){
/*<*/if(Z-1){d=S()>96;i=S()-(d?96:32) ;q++;if(p<b*5&&!S()){p+=b; q= 0;}Z=1;}/*[[*/
while(i){_=o[0][S()-97];I=_-10?b:1; for( ;I--;)putchar(_ );if (! --i||d)z(~i );}
if(p==b*5&&O){p-=b;O--;}}return 0U; }if(! (P=( f*)calloc /*]*/ (Q ,I)))return 1;
{;}for(_=p=1;p<I;p++){e(q=1);while (q< p&& strcmp( l[p ] ,l[(q)]))++ q;
t=stdin;if(q<p){(void)memcpy/* " */ (&P [p],&P [q ] ,Q);continue ;}
if(strcmp(l[p],"-")){t=fopen(l [ p] ,"rb" ) ;if(!t ){{;} ;
printf(05+*o,l[p ]);return+1; {;} }}_=b= 1<<16 ;
*&O=5;do{if(!(P[p].q=realloc (P[p].q,(P[p].P += b)+1))){return 01;}O &=72 /
6/*][*/;P[p].o+=d=fread(P[p] .q +P[ p ]. o, 1,b,t) ;}//
while(d==b) ;P [p].q[ P[ p] .o ]= 012;d =0;
e(fclose(t ) );P [p] .p =P[ p] .q;if (O)
{for(;d<P[ p] .o ;d= q+ 1) {q= d;
while(q<P[ p].o&&P[ p].q[q]- 10 ){
q++;}b=q-d; _=P [p]. d ;
if(b>_){/*]b */
P[p].d=b;}{; }
#undef/*pqdz'.*/ e// ;
#define/*s8qdb]*/e/**/0 //
//<<.<<.----.>.<<.>++.++< .[>]
/*P[*/P[p].b++;continue;}}}t= stdout;
for (p=1;p<I;p++){/**/if(P[p].b>i ){i=P[p].b;}}
if (O){for(p=0;p<i;p++){q=0;/*[*/while(I >++q){_=P[q].p-P[q ].q;
b= 0;if(_<P[q ].o){while(012-*P[q].p) {putchar(*(P[q].p++));b++;}P[q]. p++;
} ;while (P[ q].d>b++)putchar(040);} putchar(10);}return 0;}p =1;
for(; p<I ;p++)fwrite(P[p] .q,P[ p].o,1,t);return 0 ;}//
#/*] ]<. [-]<[-]<[- ]<[ -]< [- ]<;*/elif e //b
|(1 << ( __LINE__ /* >> `*//45)) | 01U
# /* */ endif //
2020-01-19 13:08:23.826 INFO 17436 --- [ main] c.n.helloworld.HelloworldApplication : Starting HelloworldApplication on LAPTOP-83O6E23J with PID 17436 (D:\Code\SpringBoot\helloworld\target\classes started by 12068 in D:\Code\SpringBoot\helloworld)
2020-01-19 13:08:23.826 INFO 17436 --- [ main] c.n.helloworld.HelloworldApplication : No active profile set, falling back to default profiles: default
2020-01-19 13:08:24.405 INFO 17436 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8088 (http)
2020-01-19 13:08:24.420 INFO 17436 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-01-19 13:08:24.420 INFO 17436 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-01-19 13:08:24.482 INFO 17436 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-01-19 13:08:24.482 INFO 17436 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 625 ms
2020-01-19 13:08:24.576 INFO 17436 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-19 13:08:24.680 INFO 17436 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''
2020-01-19 13:08:24.683 INFO 17436 --- [ main] c.n.helloworld.HelloworldApplication : Started HelloworldApplication in 1.073 seconds (JVM running for 1.635)


HELP

Getting Started

Reference Documentation

For further reference, please consider the following sections:

Guides

The following guides illustrate how to use some features concretely: