springboot官方文档
maven依赖
spring-boot-starter-parent
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.example
myproject
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
web依赖
org.springframework.boot
spring-boot-starter-web
启动类和接口
@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
@RestController = @Controller + @ResponseBody
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller //springmvc的注解
@ResponseBody //springmvc的注解
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
官方文档解释
The @RestController and @RequestMapping annotations are Spring MVC annotations. (They are not specific to Spring Boot.)
解释: @RestController 和 @RequestMapping 是springmvc的注解,没什么特殊性
@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 猜测地给开发者配置好有关springmvc、tomcat、spring方面的配置。也就是会自动配置好springmvc、tomcat、spring的一些配置
启动服务和访问接口
启动main方法,控制台打印:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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)为' '
浏览器或者http客户端(如postman)访问 localhost:8080
Hello World!
使用jar包启动
在pom.xml依赖中添加spring-boot-maven-plugin插件
org.springframework.boot
spring-boot-maven-plugin
在终端shell或者Terminal中执行命令:
$ 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 结束服务。