zoukankan      html  css  js  c++  java
  • springboot快速入门

    SpringBoot简介

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

    一、构建springboot项目

    Spring官方提供了一个在线生成springboot的项目

    地址:http://start.spring.io/

    1.打开地址,按图选择 Maven项目 -> Java语言 -> 1.5.9版本(目前1.5.9为最高的正式版本),点击 “Generate Project” 生成项目

    2、下载压缩包,解压并通过Eclipse导入(File -> Import -> Maven -> Existing Maven Projects -> 选择springboot项目 -> Finish)

     

     

     3.导入成功,添加Controller代码

     1 import java.math.BigDecimal;
     2 import org.springframework.web.bind.annotation.RequestMapping;
     3 import org.springframework.web.bind.annotation.RestController;
     4 
     5 @RestController
     6 public class CalculateController {
     7     
     8     @RequestMapping(value = "/add")
     9     public BigDecimal add(BigDecimal num1, BigDecimal num2){
    10         if(num1==null||num2==null){
    11             return BigDecimal.ZERO;
    12         }
    13         
    14         return num1.add(num2);
    15     }
    16     
    17 }

     4.启动springboot服务

    打开StartApplication类,并执行main方法(Run -> Run As -> Java Application)

      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.9.RELEASE)
    
    2018-01-05 14:53:33.286  INFO 22216 --- [           main] com.lianjinsoft.StartApplication         : Starting StartApplication on LAPTOP-1DF7S904 with PID 22216 (E:\git\spring-boot\spring-boot-start\target\classes started by lianjinsoft in E:\git\spring-boot\spring-boot-start)
    2018-01-05 14:53:33.288  INFO 22216 --- [           main] com.lianjinsoft.StartApplication         : No active profile set, falling back to default profiles: default
    2018-01-05 14:53:33.338  INFO 22216 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@50de0926: startup date [Fri Jan 05 14:53:33 CST 2018]; root of context hierarchy
    2018-01-05 14:53:35.401  INFO 22216 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2018-01-05 14:53:35.414  INFO 22216 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2018-01-05 14:53:35.415  INFO 22216 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
    2018-01-05 14:53:35.544  INFO 22216 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2018-01-05 14:53:35.544  INFO 22216 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2209 ms
    2018-01-05 14:53:35.785  INFO 22216 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2018-01-05 14:53:35.792  INFO 22216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2018-01-05 14:53:35.793  INFO 22216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2018-01-05 14:53:35.795  INFO 22216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2018-01-05 14:53:35.795  INFO 22216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2018-01-05 14:53:36.266  INFO 22216 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@50de0926: startup date [Fri Jan 05 14:53:33 CST 2018]; root of context hierarchy
    2018-01-05 14:53:36.324  INFO 22216 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/add]}" onto public java.math.BigDecimal com.lianjinsoft.controller.CalculateController.add(java.math.BigDecimal,java.math.BigDecimal)
    2018-01-05 14:53:36.327  INFO 22216 --- [           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)
    2018-01-05 14:53:36.328  INFO 22216 --- [           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)
    2018-01-05 14:53:36.372  INFO 22216 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-01-05 14:53:36.372  INFO 22216 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-01-05 14:53:36.404  INFO 22216 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-01-05 14:53:36.574  INFO 22216 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2018-01-05 14:53:36.693  INFO 22216 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2018-01-05 14:53:36.704  INFO 22216 --- [           main] com.lianjinsoft.StartApplication         : Started StartApplication in 3.68 seconds (JVM running for 4.012)

    5.验证

    打开浏览器,访问地址 http://localhost:8080/add?num1=3&num2=4

    6.测试成功,so easy~

    备注:

    1)springboot默认集成tomcat容器,通过SpringApplication.run启动

    2)springboot默认服务不带上下文路径(通过http://ip:port/访问)

    3)springboot默认端口为:8080

    源代码:https://gitee.com/skychenjiajun/spring-boot

  • 相关阅读:
    RabbitMQ安装(发生系统错误5。拒绝访问。发生系统错误1067。进程意外终止。)
    SQLServer执行脚本提示“系统找不到指定的文件”或“内存资源不足”
    TypeScript@HelloWorld!
    超详细Node安装教程
    进制转换
    菜鸟成长记
    ASP.NET Core中使用MialKit实现邮件发送
    VS未能正确加载 ”Microsoft.VisualStudio.Editor.Implementation.EditorPackate“包错误解决方法
    C#Winfrom Listview数据导入Excel
    安装研发服务器
  • 原文地址:https://www.cnblogs.com/skychenjiajun/p/8204314.html
Copyright © 2011-2022 走看看