https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#_working_with_spring_boothtml
spring-boot-starter-parentjava
<?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依賴git
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
@RestController @EnableAutoConfiguration public class HelloWorldApp { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(HelloWorldApp.class, args); } }
@RestController = @Controller + @ResponseBodyweb
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller //springmvc的註解 @ResponseBody //springmvc的註解 public @interface RestController { @AliasFor( annotation = Controller.class ) String value() default ""; }
官方文檔解釋spring
The @RestController and @RequestMapping annotations are Spring MVC annotations. (They are not specific to Spring Boot.) 解釋: @RestController 和 @RequestMapping 是springmvc的註解,沒什麼特殊性
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 猜想地給開發者配置好有關springmvc、tomcat、spring方面的配置。也就是會自動配置好springmvc、tomcat、spring的一些配置
啓動main方法,控制檯打印:shell
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: 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)
日誌中看到 項目啓動端口(prot)爲8080,啓動根路徑(path)爲' 'apache
瀏覽器或者http客戶端(如postman)訪問 localhost:8080瀏覽器
Hello World!
在pom.xml依賴中添加spring-boot-maven-plugin插件tomcat
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
在終端shell或者Terminal中執行命令:springboot
$ mvn package
在項目target目錄下會生成jar包:
啓動jar包
$ 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)
訪問 localhost:8080
Hello World!
按 Ctrl + C 結束服務。
$ mvn spring-boot:run