Preface
I have been using SpringBoot for several years, and I have never summarized its related usage and techniques. This time, I have plenty of time, so I just summarized the related usage techniques to facilitate the quick construction and reference of subsequent projects.
This chapter is the first in the SpringBoot usage series, quickStart,
Pre-operation
- JDK
- Maven
- IDEA
Related Actions - QuickStart
Add related dependency packages.
Because JDK8 is used as the main development environment, the latest version is not used SpringBoot 3.0
. The teaching content in this chapter uses SpringBoot 2.3.12RELEASE
the main SpringBoot
version.
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<springboot.version>2.3.12.RELEASE</springboot.version>
<lombock.version>1.18.12</lombock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
Startup class
package com.yanxml.arsenal.springboot.quickstart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"com.yanxml.arsenal.springboot"})
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}
}
Among them, there is mainly 1 annotation, @SpringBootApplication
annotation.
- SpringBootApplication is a combination of several conforming tags. It
scanBasePackages
mainly specifies the package name that needs to be scanned for automatic injection of beans. application.run(args);
. If we use it at startup and pass parameters in args, we can get it through the singleton Bean of the Environment class.
Controller demo class
package com.yanxml.arsenal.springboot.quickstart.controller;
import org.springframework.web.bind.annotation.*;
/**
* HelloWorld Controller.
*
* @author seanyanxml
* @date 2023-01-31
*/
@RestController
@RequestMapping(path = "/helloworld")
public class HelloWorldController {
@RequestMapping(path = "/get", method = RequestMethod.GET)
public String testGetMethod() {
return "hello";
}
}
There are mainly 2 label @RequestMapping
annotations, @RestController
annotations,
@RestController
It is also composed of multiple annotations.@RequestMapping
, mainly including functions such as the address of the mapping method, the type of HTTPMethod of the mapping method, etc. In the SpringMVC era, it was necessary to manually specify the mapping relationship. The emergence of SpringBoot simplified the manual writing process.
application.yml configuration file
server:
port: 8080
servlet:
context-path: /arsenal-springboot
Since this chapter is the initial startup of the project, there are few related configurations. There are only 2 related configurations.
server.port
Used to specify the port number used by the springboot-web program.server.servlet
A unified prefix used to specify the access address of the springboot-web program.
Launch & Test
run Application.class
D:\develop\jdk\jdk_8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always "-javaagent:D:\develop\IDEA\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=6930:D:\develop\IDEA\IntelliJ IDEA 2021.1.3\bin" -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Dfile.encoding=UTF-8 -classpath D:\develop\jdk\jdk_8\jre\lib\charsets.jar;D:\develop\jdk\jdk_8\jre\lib\deploy.jar;D:\develop\jdk\jdk_8\jre\lib\ext\access-bridge-64.jar;D:\develop\jdk\jdk_8\jre\lib\ext\cldrdata.jar;D:\develop\jdk\jdk_8\jre\lib\ext\dnsns.jar;D:\develop\jdk\jdk_8\jre\lib\ext\jaccess.jar;D:\develop\jdk\jdk_8\jre\lib\ext\jfxrt.jar;D:\develop\jdk\jdk_8\jre\lib\ext\localedata.jar;D:\develop\jdk\jdk_8\jre\lib\ext\nashorn.jar;D:\develop\jdk\jdk_8\jre\lib\ext\sunec.jar;D:\develop\jdk\jdk_8\jre\lib\ext\sunjce_provider.jar;D:\develop\jdk\jdk_8\jre\lib\ext\sunmscapi.jar;D:\develop\jdk\jdk_8\jre\lib\ext\sunpkcs11.jar;D:\develop\jdk\jdk_8\jre\lib\ext\zipfs.jar;D:\develop\jdk\jdk_8\jre\lib\javaws.jar;D:\develop\jdk\jdk_8\jre\lib\jce.jar;D:\develop\jdk\jdk_8\jre\lib\jfr.jar;D:\develop\jdk\jdk_8\jre\lib\jfxswt.jar;D:\develop\jdk\jdk_8\jre\lib\jsse.jar;D:\develop\jdk\jdk_8\jre\lib\management-agent.jar;D:\develop\jdk\jdk_8\jre\lib\plugin.jar;D:\develop\jdk\jdk_8\jre\lib\resources.jar;D:\develop\jdk\jdk_8\jre\lib\rt.jar;D:\repository\arsenal-public\arsenal-springboot-usage\arsenal-springboot-quickstart\target\classes;C:\Users\sean\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.3.12.RELEASE\spring-boot-starter-web-2.3.12.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\boot\spring-boot-starter\2.3.12.RELEASE\spring-boot-starter-2.3.12.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\boot\spring-boot\2.3.12.RELEASE\spring-boot-2.3.12.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.12.RELEASE\spring-boot-autoconfigure-2.3.12.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.3.12.RELEASE\spring-boot-starter-logging-2.3.12.RELEASE.jar;C:\Users\sean\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\sean\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\sean\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\sean\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;C:\Users\sean\.m2\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;C:\Users\sean\.m2\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;C:\Users\sean\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\sean\.m2\repository\org\springframework\spring-core\5.2.15.RELEASE\spring-core-5.2.15.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\spring-jcl\5.2.15.RELEASE\spring-jcl-5.2.15.RELEASE.jar;C:\Users\sean\.m2\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\Users\sean\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.3.12.RELEASE\spring-boot-starter-json-2.3.12.RELEASE.jar;C:\Users\sean\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.11.4\jackson-databind-2.11.4.jar;C:\Users\sean\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.4\jackson-annotations-2.11.4.jar;C:\Users\sean\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.11.4\jackson-core-2.11.4.jar;C:\Users\sean\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.4\jackson-datatype-jdk8-2.11.4.jar;C:\Users\sean\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.4\jackson-datatype-jsr310-2.11.4.jar;C:\Users\sean\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.4\jackson-module-parameter-names-2.11.4.jar;C:\Users\sean\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.3.12.RELEASE\spring-boot-starter-tomcat-2.3.12.RELEASE.jar;C:\Users\sean\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.46\tomcat-embed-core-9.0.46.jar;C:\Users\sean\.m2\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;C:\Users\sean\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.46\tomcat-embed-websocket-9.0.46.jar;C:\Users\sean\.m2\repository\org\springframework\spring-web\5.2.15.RELEASE\spring-web-5.2.15.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\spring-beans\5.2.15.RELEASE\spring-beans-5.2.15.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\spring-webmvc\5.2.15.RELEASE\spring-webmvc-5.2.15.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\spring-aop\5.2.15.RELEASE\spring-aop-5.2.15.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\spring-context\5.2.15.RELEASE\spring-context-5.2.15.RELEASE.jar;C:\Users\sean\.m2\repository\org\springframework\spring-expression\5.2.15.RELEASE\spring-expression-5.2.15.RELEASE.jar;C:\Users\sean\.m2\repository\org\projectlombok\lombok\1.18.12\lombok-1.18.12.jar com.yanxml.arsenal.springboot.quickstart.Application
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.12.RELEASE)
2023-01-31 23:55:40.836 INFO 6296 --- [ main] c.y.a.springboot.quickstart.Application : Starting Application on LAPTOP-BQK5NDTK with PID 6296 (D:\repository\arsenal-public\arsenal-springboot-usage\arsenal-springboot-quickstart\target\classes started by sean in D:\repository\arsenal-public\arsenal-springboot-usage)
2023-01-31 23:55:40.836 INFO 6296 --- [ main] c.y.a.springboot.quickstart.Application : No active profile set, falling back to default profiles: default
2023-01-31 23:55:41.508 INFO 6296 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-01-31 23:55:41.524 INFO 6296 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-01-31 23:55:41.524 INFO 6296 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2023-01-31 23:55:41.696 INFO 6296 --- [ main] o.a.c.c.C.[.[.[/arsenal-springboot] : Initializing Spring embedded WebApplicationContext
2023-01-31 23:55:41.696 INFO 6296 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 813 ms
2023-01-31 23:55:41.821 INFO 6296 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-01-31 23:55:41.946 INFO 6296 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/arsenal-springboot'
2023-01-31 23:55:41.961 INFO 6296 --- [ main] c.y.a.springboot.quickstart.Application : Started Application in 1.388 seconds (JVM running for 2.334)
Test HelloWorld
- Access address Get method
localhost:8080/arsenal-springboot//helloworld/get - You can get the following responses
hello