Spring基礎:快速入門spring boot(2):SPRING INITIALIZR
阿新 • • 發佈:2018-11-02
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
SPRING INITIALIZR是spring boot在構建專案時候非常有效, 雖然說Maven以及Spring boot提供的starter使用起來非常簡單, 但是由於元件和關聯部分眾多,有這樣一個視覺化的配置構建管理工具仍然很不錯. 在這篇文章中我們將會在IntelliJ IDEA中利用SPRING INITIALIZR來建立一個Hello World的例子程式, 不包括Maven下載的速度的話, 1分鐘應該夠了.
建立一個SPRING INITIALIZR專案
使用IntelliJ IDEA建立一個SPRING INITIALIZR型別的專案,除了選擇web設定之外,一切全部Default。
選擇Web
如果沒有IntelliJ使用STS建立Starter型別的也是一樣方便。二者都沒有可以使用如下連線生成一個初始的專案zip包都是可以的。
URL | https://start.spring.io/ |
---|
專案詳細
建立之後安靜地等待Maven把所有的依賴全部下載下來。抽空看一下建立的內容。
可以看到,右側程式碼區加亮部分正是SpringBoot的入口呼叫。
而左側下方則是Maven辛辛苦苦替我們下載的內容。
Pom檔案
Pom.xml檔案如下所示
<?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>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
可以在的pendency中清楚地看到我們加入的Web部分。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
- 1
- 2
- 3
- 4
修改DemoApplication
加入如下兩行內容
@RequestMapping("/") public String Hello(){ return "Hello, Spring Boot..."; }
- 1
- 2
- 3
- 4
同時對DemoApplication加上RestController註解,修改後的source如下
package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class DemoApplication { @RequestMapping("/") public String Hello(){ return "Hello, Spring Boot..."; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
執行
右鍵執行即可
結果確認
執行起來後就可以在瀏覽器中檢視結果了
Console輸出
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.2.RELEASE)2016-12-03 08:57:58.888 INFO 5128 --- [ main] com.example.DemoApplication : Starting DemoApplication on vcc-PC with PID 5128 (C:\Users\Administrator\IdeaProjects\demo\target\classes started by Administrator in C:\Users\Administrator\IdeaProjects\demo)2016-12-03 08:57:58.891 INFO 5128 --- [ main] com.example.DemoApplication : No active profile set, falling back to default profiles: default2016-12-03 08:57:58.949 INFO 5128 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]30ee2816: startup date [Sat Dec 03 08:57:58 CST 2016]; root of context hierarchy2016-12-03 08:58:00.512 INFO 5128 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2016-12-03 08:58:00.525 INFO 5128 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat2016-12-03 08:58:00.526 INFO 5128 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.62016-12-03 08:58:00.624 INFO 5128 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2016-12-03 08:58:00.624 INFO 5128 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1678 ms2016-12-03 08:58:00.740 INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2016-12-03 08:58:00.744 INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2016-12-03 08:58:00.744 INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2016-12-03 08:58:00.744 INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2016-12-03 08:58:00.744 INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2016-12-03 08:58:00.962 INFO 5128 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]30ee2816: startup date [Sat Dec 03 08:57:58 CST 2016]; root of context hierarchy2016-12-03 08:58:01.018 INFO 5128 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.example.DemoApplication.Hello()2016-12-03 08:58:01.022 INFO 5128 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2016-12-03 08:58:01.022 INFO 5128 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)2016-12-03 08:58:01.043 INFO 5128 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-12-03 08:58:01.043 INFO 5128 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-12-03 08:58:01.071 INFO 5128 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-12-03 08:58:01.174 INFO 5128 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2016-12-03 08:58:01.226 INFO 5128 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2016-12-03 08:58:01.230 INFO 5128 --- [ main] com.example.DemoApplication : Started DemoApplication in 2.751 seconds (JVM running for 3.109)2016-12-03 08:58:06.041 INFO 5128 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'2016-12-03 08:58:06.041 INFO 5128 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2016-12-03 08:58:06.052 INFO 5128 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 11 ms
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35