spring boot学习总结(一)-- 基础入门 Hello,spring boot!
写在最前
SpringBoot是伴随着Spring4.0诞生的;
从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架;
SpringBoot帮助开发者快速启动一个Web容器;
SpringBoot继承了原有Spring框架的优秀基因;
SpringBoot简化了使用Spring的过程。
SpringBoot使编码变简单,使配置变简单,使部署变简单,使监控变简单
下面正式开始!!!快速入门!!!
1、构建Maven项目
(1)访问 http://start.spring.io/,填写信息,选择版本
(2)点击 Switch to the full version
(3)选择适合的 Java Version
(4)点击 Generate Project,下载项目,可以得到项目压缩包
(5)解压项目压缩包,然后Import
完整的项目结构(除了controller包是下面第2步手动建的之外,其他都是下载后就有的)
打开pom.xml,可以看到下列代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.lmei</groupId>
<artifactId>SpringBoot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <name>SpringBoot01</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
Import项目后,如果pom.xml报错,重新保存,让它重新加载。
加载成功后,pom.xml不再报错,但项目报错“Could not calculate build plan..."
右键项目->Maven->Update Project Configuration
2、测试
新建一个HelloController.java
package com.lmei.SpringBoot01.controller; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@EnableAutoConfiguration
public class HelloController { @RequestMapping("/hello")
@ResponseBody
public String home() {
return "Hello ,spring boot!";
} public static void main(String[] args) throws Exception {
SpringApplication.run(HelloController.class, args);
//运行之后在浏览器中访问:http://localhost:8080/hello
}
}
运行,HelloController右键->Run As->Java Application
控制台输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.4.RELEASE) -- ::59.840 INFO --- [ main] c.l.S.controller.HelloController : Starting HelloController on USER-PC with PID (D:\workspace_spring\SpringBoot01\target\classes started by USER in D:\workspace_spring\SpringBoot01)
-- ::59.840 INFO --- [ main] c.l.S.controller.HelloController : No active profile set, falling back to default profiles: default
-- ::59.902 INFO --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@524b19: startup date [Sat Jul :: CST ]; root of context hierarchy
-- ::01.961 INFO --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): (http)
-- ::01.977 INFO --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
-- ::01.977 INFO --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.
-- ::02.196 INFO --- [ost-startStop-] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
-- ::02.196 INFO --- [ost-startStop-] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in ms
-- ::02.446 INFO --- [ost-startStop-] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
-- ::02.462 INFO --- [ost-startStop-] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-22 20:11:02.462 INFO 6484 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-22 20:11:02.462 INFO 6484 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-22 20:11:02.462 INFO 6484 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-22 20:11:02.898 INFO 6484 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@524b19: startup date [Sat Jul 22 20:10:59 CST 2017]; root of context hierarchy
2017-07-22 20:11:03.008 INFO 6484 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto java.lang.String com.lmei.SpringBoot01.controller.HelloController.home()
2017-07-22 20:11:03.008 INFO 6484 --- [ 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-07-22 20:11:03.008 INFO 6484 --- [ 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-07-22 20:11:03.071 INFO 6484 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-22 20:11:03.071 INFO 6484 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-22 20:11:03.133 INFO 6484 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
-- ::03.352 INFO --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
-- ::03.445 INFO --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): (http)
-- ::03.445 INFO --- [ main] c.l.S.controller.HelloController : Started HelloController in 4.137 seconds (JVM running for 4.651)
-- ::39.768 INFO --- [nio--exec-] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
-- ::39.769 INFO --- [nio--exec-] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
-- ::39.819 INFO --- [nio--exec-] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in ms
然后在浏览器访问:http://localhost:8080/hello
运行时,可能会出现的错误
unable to start embedded container; nested exception is java.lang.NoSuchMethodError:
org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;
完整的错误信息如下:
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)V
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:) ~[spring-context-4.3..RELEASE.jar:4.3..RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:) [spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:) [spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at com.lmei.SpringBoot01.controller.HelloController.main(HelloController.java:) [classes/:na]
Caused by: java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)V
at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner$Tomcat8TldSkipSetter.setSkipPattern(SkipPatternJarScanner.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.setPatternToTomcat8SkipFilter(SkipPatternJarScanner.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.<init>(SkipPatternJarScanner.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.apply(SkipPatternJarScanner.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.prepareContext(TomcatEmbeddedServletContainerFactory.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:) ~[spring-boot-1.5..RELEASE.jar:1.5..RELEASE]
... common frames omitted
出错的原因是因为配置的时候不小心把tomcat配置进去,去掉就可以
本文分享到此~
spring boot学习总结(一)-- 基础入门 Hello,spring boot!的更多相关文章
- Spring boot学习1 构建微服务:Spring boot 入门篇
Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- 学习《零基础入门学习Python》电子书PDF+笔记+课后题及答案
初学python入门建议学习<零基础入门学习Python>.适合新手入门,很简单很易懂.前一半将语法,后一半讲了实际的应用. Python3入门必备,小甲鱼手把手教授Python,包含电子 ...
- 006 SpringCloud 学习笔记2-----SpringCloud基础入门
1.SpringCloud概述 微服务是一种架构方式,最终肯定需要技术架构去实施. 微服务的实现方式很多,但是最火的莫过于Spring Cloud了.SpringCloud优点: - 后台硬:作为Sp ...
- Spring Boot 2.x零基础入门到高级实战教程
一.零基础快速入门SpringBoot2.0 1.SpringBoot2.x课程全套介绍和高手系列知识点 简介:介绍SpringBoot2.x课程大纲章节 java基础,jdk环境,maven基础 2 ...
- 《马哥出品高薪linux运维教程》wingkeung学习笔记-linux基础入门课程
计算机原理概念: 1.CPU和内存中的存储单元通信线路称为总线(BUS),总线是被指令和数据复用的,所以也称为前端总线. 2.计算机中计算频率的时间标准即晶体振荡器原理,精确计算时间长度,根据相同的时 ...
- spring boot学习笔记(二)创建spring boot项目
用eclipse(需要用高版本,要不然弄不出来):new →Spring Sarter Project 用IDEA:一般默认 一般默认 入门级的先 剩下的一般默认... 一.项目至少有下面的东西,里面 ...
- Java框架spring Boot学习笔记(六):Spring Boot事务管理
SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.
- Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查
在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...
- Java框架spring Boot学习笔记(四):Spring Boot操作MySQL数据库
在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...
随机推荐
- str和unicode类
在py2中,分为两类,str和unicode 而在py3中,分为两类,byte和str py2中的str等同于py3中的byte 首先明确一点,我们编辑好一段文本,python并不知道我们的文本是以什 ...
- Java基本数据类型装箱的127临界点
package wrapper.demo; public class WrapperDemo { /** * @param args */ public static void main(String ...
- Java练习之使用StringBuilder
package string.demo; /* * 将数组变为字符串输出 */ public class StringBuilderTest { /** * @param args */ public ...
- 洛谷 P4656: LOJ 2484: [CEOI2017]Palindromic Partitions
菜菜只能靠写简单字符串哈希维持生活. 题目传送门:LOJ #2484. 题意简述: 题面讲得很清楚了. 题解: 很显然从两边往中间推,能选的就选上这个贪心策略是对的. 如何判断能不能选上,直接字符串哈 ...
- 『Matplotlib』数据可视化专项
一.相关知识 官网介绍 matplotlib API 相关博客 matplotlib绘图基础 漂亮插图demo 使用seaborn绘制漂亮的热度图 fig, ax = plt.subplots(2,2 ...
- mysql学习------二进制日志
一.什么是二进制日志 1.记录对数据发生或潜在发生更改的sql语句 2.二进制格式保存 3.用途广泛,包括 a.查看数据库变更历史 b.数据库增量备份 c.数据库灾难恢复 d.mysql replic ...
- 关于overflow: hidden;的一个诡异问题
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- MongoDB学习笔记-2(使用java连接Mongo)
本人使用maven创建的java工程,mongo-java-driver使用的是3.2.2,这个驱动2.x和3.x是有差异的 pom.xml配置加入: <dependency> <g ...
- DedeCMS常见问题和技巧
1: dedecms 访问空白(织梦如何显示详细错误) 我们在使用织梦的时候,有的时候会遇到访问空白的情况,尤其是再刚刚搬家之后,织梦会出现访问空白或者返给您一个500的友好界面错误,遇到这种情况该怎 ...
- tidb 升级步骤
1.检查ansible版本,正常情况下,2.1 rc3需要兼容ansible 2.5以上的版本 $ ansible --version 2.检查python两个模块jinja2需要升级到2.9.6或以 ...