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” 生成项目

springboot快速入门

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

springboot快速入门

springboot快速入门

springboot快速入门

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

 import java.math.BigDecimal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class CalculateController { @RequestMapping(value = "/add")
public BigDecimal add(BigDecimal num1, BigDecimal num2){
if(num1==null||num2==null){
return BigDecimal.ZERO;
} return num1.add(num2);
} }

4.启动springboot服务

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

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.9.RELEASE) -- ::33.286 INFO --- [ main] com.lianjinsoft.StartApplication : Starting StartApplication on LAPTOP-1DF7S904 with PID (E:\git\spring-boot\spring-boot-start\target\classes started by lianjinsoft in E:\git\spring-boot\spring-boot-start)
-- ::33.288 INFO --- [ main] com.lianjinsoft.StartApplication : No active profile set, falling back to default profiles: default
-- ::33.338 INFO --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@50de0926: startup date [Fri Jan :: CST ]; root of context hierarchy
-- ::35.401 INFO --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): (http)
-- ::35.414 INFO --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
-- ::35.415 INFO --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.
-- ::35.544 INFO --- [ost-startStop-] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
-- ::35.544 INFO --- [ost-startStop-] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in ms
-- ::35.785 INFO --- [ost-startStop-] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
-- ::35.792 INFO --- [ost-startStop-] 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]
-- ::36.574 INFO --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
-- ::36.693 INFO --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): (http)
-- ::36.704 INFO --- [ main] com.lianjinsoft.StartApplication : Started StartApplication in 3.68 seconds (JVM running for 4.012)

5.验证

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

springboot快速入门

6.测试成功,so easy~

备注:

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

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

3)springboot默认端口为:8080

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

上一篇:掌握C++基础


下一篇:Android为TV端助力 最简单的自定义圆点view