Springboot 學習筆記 ①

來源:互聯網
上載者:User

標籤:str   groups   技術分享   mbed   start   建立   相對路徑   put   imp   

前言

之前一直在尋找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;@RestControllerpublic 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: default2017-10-03 23:28:58.483  INFO 9436 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]351d0846: startup date [Tue Oct 03 23:28:58 CST 2017]; root of context hierarchy2017-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.202017-10-03 23:29:01.696  INFO 9436 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2017-10-03 23:29:01.696  INFO 9436 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3219 ms2017-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[email protected]351d0846: startup date [Tue Oct 03 23:28:58 CST 2017]; root of context hierarchy2017-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 startup2017-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 started2017-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的測試程式就大功告成啦,感謝大家的觀看,祝大家國慶快樂!!  ^_^

Springboot 學習筆記 ①

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.