springboot official document
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#_working_with_spring_boot
maven dependence
spring-boot-starter-parent
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> </parent> <!-- Additional lines to be added here... --> </project>
web dependence
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Startup classes and interfaces
@RestController @EnableAutoConfiguration public class HelloWorldApp { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(HelloWorldApp.class, args); } }
Explanatory notes
@RestController
@RestController = @Controller + @ResponseBody
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller //Annotations to spring MVC @ResponseBody //Annotations to spring MVC public @interface RestController { @AliasFor( annotation = Controller.class ) String value() default ""; }
Official Document Interpretation
The @RestController and @RequestMapping annotations are Spring MVC annotations. (They are not specific to Spring Boot.) Explanation: @RestController and @RequestMapping are annotations for spring mvc, with no specificity
@EnableAutoConfiguration
This annotation tells Spring Boot to "guess" how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly. @EnableAutoConfiguration Conjecturally configure the developer springmvc,tomcat,spring Configuration of aspects. That is to say, it will be automatically configured. springmvc,tomcat,spring Some configurations
Start service and access interface
Start main method, console print:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.0.RELEASE) 2018-11-21 15:03:33.622 INFO 75693 --- [ main] com.yimingkeji.helloworld.HelloWorldApp : Starting HelloWorldApp on yangzhenlongdeMacBook-Pro.local with PID 75693 (/Users/yangzhenlong/projs/my/yimingkeji/springboot/helloword/target/classes started by yangzhenlong in /Users/yangzhenlong/projs/my/yimingkeji/springboot) 2018-11-21 15:03:33.626 INFO 75693 --- [ main] com.yimingkeji.helloworld.HelloWorldApp : No active profile set, falling back to default profiles: default 2018-11-21 15:03:34.499 INFO 75693 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-11-21 15:03:34.516 INFO 75693 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-11-21 15:03:34.516 INFO 75693 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12 2018-11-21 15:03:34.522 INFO 75693 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/yangzhenlong/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] 2018-11-21 15:03:34.581 INFO 75693 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-11-21 15:03:34.582 INFO 75693 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 917 ms 2018-11-21 15:03:34.604 INFO 75693 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-11-21 15:03:34.607 INFO 75693 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-11-21 15:03:34.608 INFO 75693 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-11-21 15:03:34.608 INFO 75693 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*] 2018-11-21 15:03:34.608 INFO 75693 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-11-21 15:03:34.763 INFO 75693 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2018-11-21 15:03:34.933 INFO 75693 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-11-21 15:03:34.936 INFO 75693 --- [ main] com.yimingkeji.helloworld.HelloWorldApp : Started HelloWorldApp in 1.727 seconds (JVM running for 2.849)
You can see in the log that the project startup port (prot) is 8080 and the startup root path (path) is''
Browsers or http clients (such as postman) access localhost:8080
Hello World!
Start with a jar package
Add spring-boot-maven-plugin plug-in to pom.xml dependencies
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Execute commands in the Terminal shell or Terminal:
$ mvn package
The jar package is generated in the project target directory:
Start jar package
$ java -jar target/helloword-1.0.0-SNAPSHOT.jar 2018-11-21 15:12:35.077 INFO 75841 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2018-11-21 15:12:35.355 INFO 75841 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-11-21 15:12:35.359 INFO 75841 --- [ main] com.yimingkeji.helloworld.HelloWorldApp : Started HelloWorldApp in 2.382 seconds (JVM running for 2.838)
Visit localhost:8080
Hello World!
Press Ctrl + C to end the service.
Start with mvn
$ mvn spring-boot:run