1.生成springboot项目
springboot项目有两种生成的方法,一是通过https://start.spring.io/网站,自定义生成,另外就是通过idea创建springboot项目。个人觉得方式一特别方便。操作如下:
我们只需要打开https://start.spring.io/网站:
选择合适的项目打开方式,和项目的metadata进行配置:
image.png
此外还能方便的加入依赖的包。
之后点击generate就能产生一个目录完备的zip包。
image.png
在build.gradle文件中,已经将我们需要的依赖jar包都导入了:
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.dhb'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
我们只需要通过idea导入该项目即可。
2.springboot中的helloword
在生成的文件中,已经自动创建了MySpringBootApplication.java文件在之前定义的package下:
package com.dhb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
现在需要定义一个/hello的url地址,当浏览器打开的时候,返回hello word。
有两种方式可以实现,分别是通过@Controller注解,代码如下:
package com.dhb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "hello";
}
}
此外,还有一种方式是通过RestContr实现:
package com.dhb.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestHelloController {
@RequestMapping("/resthello")
public String hello() {
return "hello word!";
}
}
实际上,@RestHelloController等价于@Controller与@ResponseBody的合体。
之后运行MySpringBootApplication中的main函数:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-01-28 18:46:29.945 INFO 26860 --- [ restartedMain] com.dhb.MySpringBootApplication : Starting MySpringBootApplication using Java 1.8.0_231 on DESKTOP-HR38DGU with PID 26860 (D:\workspace-mashibing\MySpringBoot\build\classes\java\main started by Administrator in D:\workspace-mashibing\MySpringBoot)
2021-01-28 18:46:29.949 INFO 26860 --- [ restartedMain] com.dhb.MySpringBootApplication : No active profile set, falling back to default profiles: default
2021-01-28 18:46:29.994 INFO 26860 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-01-28 18:46:29.994 INFO 26860 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-01-28 18:46:30.722 INFO 26860 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-01-28 18:46:30.730 INFO 26860 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-01-28 18:46:30.730 INFO 26860 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-01-28 18:46:30.788 INFO 26860 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-01-28 18:46:30.788 INFO 26860 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 793 ms
2021-01-28 18:46:30.927 INFO 26860 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-28 18:46:31.049 INFO 26860 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-01-28 18:46:31.071 INFO 26860 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-01-28 18:46:31.078 INFO 26860 --- [ restartedMain] com.dhb.MySpringBootApplication : Started MySpringBootApplication in 1.429 seconds (JVM running for 2.182)
服务启动完成,我们分别访问:
http://127.0.0.1:8080/hello
http://127.0.0.1:8080/resthello
都能收到正确的响应。helloword成功。
3.自定义Banner
我们可以看到启动spirng的时候,会出现一个Banner:
image.png
实际上这个banner是可以手动更换的。
如果要自定义一个banner,那么需要通过https://www.bootschool.net/ascii 定制化一个banner。
_ _ _ _ _
| |_ ___ | | | | ___ o O O __ __ __ ___ _ _ __| | o O O | |
| ' \ / -_) | | | | / _ \ o \ V V / / _ \ | '_| / _` | o |_|
|_||_| \___| _|_|_ _|_|_ \___/ TS__[O] \_/\_/ \___/ _|_|_ \__,_| TS__[O] _(_)_
_|"""""|_|"""""|_|"""""|_|"""""|_|"""""| {======|_|"""""|_|"""""|_|"""""|_|"""""| {======|_| """ |
"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'./o--000'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'./o--000'"`-0-0-'
生成了上述hello word的banner。那么下一步,对spring中的banner进行替换。
在resource中创建一个banner.txt的文件,将上述内容粘贴,这样我们就可以看到最终的启动banner得到了更换:
_ _ _ _ _
| |_ ___ | | | | ___ o O O __ __ __ ___ _ _ __| | o O O | |
| ' \ / -_) | | | | / _ \ o \ V V / / _ \ | '_| / _` | o |_|
|_||_| \___| _|_|_ _|_|_ \___/ TS__[O] \_/\_/ \___/ _|_|_ \__,_| TS__[O] _(_)_
_|"""""|_|"""""|_|"""""|_|"""""|_|"""""| {======|_|"""""|_|"""""|_|"""""|_|"""""| {======|_| """ |
"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'./o--000'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'./o--000'"`-0-0-'
2021-01-28 18:53:31.164 INFO 28780 --- [ restartedMain] com.dhb.MySpringBootApplication : Starting MySpringBootApplication using Java 1.8.0_231 on DESKTOP-HR38DGU with PID 28780 (D:\workspace-mashibing\MySpringBoot\build\classes\java\main started by Administrator in D:\workspace-mashibing\MySpringBoot)
2021-01-28 18:53:31.167 INFO 28780 --- [ restartedMain] com.dhb.MySpringBootApplication : No active profile set, falling back to default profiles: default
2021-01-28 18:53:31.212 INFO 28780 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-01-28 18:53:31.212 INFO 28780 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-01-28 18:53:31.890 INFO 28780 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-01-28 18:53:31.896 INFO 28780 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-01-28 18:53:31.896 INFO 28780 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-01-28 18:53:31.946 INFO 28780 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-01-28 18:53:31.946 INFO 28780 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 733 ms
2021-01-28 18:53:32.082 INFO 28780 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-28 18:53:32.199 INFO 28780 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-01-28 18:53:32.223 INFO 28780 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-01-28 18:53:32.230 INFO 28780 --- [ restartedMain] com.dhb.MySpringBootApplication : Started MySpringBootApplication in 1.41 seconds (JVM running for 2.261)
网友评论