Springboot 學習筆記 ①

分類:IT技術 時間:2017-10-05

前言

之前一直在尋找Springboot的學習資料,終於得償所願。。。那麽,先給自己定一個小目標 - 能夠使用Springboot這套架構來搭建自己的服務。

 

準備階段

1. 開發環境

開發環境其實還是因人而異的(官方的說法是Java7以上,推薦Java8;貌似也可以用Java6)。lz這裏的平臺為Windows,Jdk版本為1.8,maven版本為3.39。具體信息可以看下圖

 

 

2. 為maven配置aliyun鏡像倉庫

由於眾所周知的原因,我們訪問maven官方的網站的時候,速度沒有辣麽“給力”,所以通過aliyun的鏡像倉庫來“加速”。具體的配置方法也很簡單,就是在".m2"目錄下的“setting.xml”文件中加入如下配置:

<mirrors>
         <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>*</mirrorOf>
            <name>Nexus aliyun</name>   
<url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>

 

3. IDE

我這裏使用的是STS,版本為: 3.8.4

 

 

創建第一個Springboot程序(Restful Web Service)

好了,前面啰嗦了一大堆,終於進入正題啦。。。

 1. 新建一個spring start project 

 

 2. 選擇建立一個Web項目

選擇完成後,點擊“finish”按鈕,之後STS就會開始下載項目依賴的項目;待所有東西就緒之後,我們可以看到項目的結構如圖所示:

 

3. 編寫controller類,處理Http request

package com.example.HelloSpringBoot.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloSpringBootController {

    @RequestMapping("/hello")
    public String hello(){
        return "Hello spring boot!";
    }
}

@RestController - 該註解表示類 “
HelloSpringBootController”是一個web controller,它用來處理web request
@RequestMapping("/hello") - 該註解表明請求的相對路徑為 “/hello”

至此,我們的編碼工作已經完成。

 

4. 啟動Springboot項目

選中默認生成的文件“HelloSpringBootApplication.java” -> 右鍵 -> "run as spring boot app"

啟動後,我們會在console裏面看到如下輸出

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.7.RELEASE)

2017-10-03 23:28:58.291  INFO 9436 --- [           main] c.e.H.HelloSpringBootApplication         : Starting HelloSpringBootApplication on Tuo-PC with PID 9436 (E:\sts-bundle\sts-3.8.4.RELEASE\workspace_springboot\HelloSpringBoot\target\classes started by Tuo in E:\sts-bundle\sts-3.8.4.RELEASE\workspace_springboot\HelloSpringBoot)
2017-10-03 23:28:58.298  INFO 9436 --- [           main] c.e.H.HelloSpringBootApplication         : No active profile set, falling back to default profiles: default
2017-10-03 23:28:58.483  INFO 9436 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@351d0846: startup date [Tue Oct 03 23:28:58 CST 2017]; root of context hierarchy
2017-10-03 23:29:01.261  INFO 9436 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-10-03 23:29:01.298  INFO 9436 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-10-03 23:29:01.303  INFO 9436 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.20
2017-10-03 23:29:01.696  INFO 9436 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-10-03 23:29:01.696  INFO 9436 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3219 ms
2017-10-03 23:29:02.216  INFO 9436 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-10-03 23:29:02.227  INFO 9436 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-10-03 23:29:02.230  INFO 9436 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-10-03 23:29:02.231  INFO 9436 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-10-03 23:29:02.231  INFO 9436 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-10-03 23:29:02.968  INFO 9436 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@351d0846: startup date [Tue Oct 03 23:28:58 CST 2017]; root of context hierarchy
2017-10-03 23:29:03.114  INFO 9436 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.example.HelloSpringBoot.Controller.HelloSpringBootController.hello()
2017-10-03 23:29:03.122  INFO 9436 --- [           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)
2017-10-03 23:29:03.123  INFO 9436 --- [           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)
2017-10-03 23:29:03.182  INFO 9436 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-03 23:29:03.182  INFO 9436 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-03 23:29:03.260  INFO 9436 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-03 23:29:03.628  INFO 9436 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-10-03 23:29:03.832  INFO 9436 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-10-03 23:29:03.842  INFO 9436 --- [           main] c.e.H.HelloSpringBootApplication         : Started HelloSpringBootApplication in 6.118 seconds (JVM running for 7.13)
2017-10-03 23:29:27.484  INFO 9436 --- [nio-8080-exec-3] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-10-03 23:29:27.484  INFO 9436 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-10-03 23:29:27.515  INFO 9436 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 31 ms

 

 通過輸出的內容,我們可以看到Springboot程序已經成功啟動!

 

5. 通過瀏覽器,發送web request,驗證程序。

 

 

OK,我們的第一個基於Springboot的測試程序就大功告成啦,感謝大家的觀看,祝大家國慶快樂!!  ^_^


Tags: Springboot amp gt lt 項目 aliyun

文章來源:


ads
ads

相關文章
ads

相關文章

ad