Springboot 学习笔记 ①

前言

之前一直在寻找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的测试程序就大功告成啦,感谢大家的观看,祝大家国庆快乐!!  ^_^

posted @ 2017-10-03 23:37  TonyZhang24  阅读(835)  评论(0编辑  收藏  举报