spring boot入门篇,helloworld案例演示
为什么用spring boot?
嵌入的 Tomcat,无需部署 WAR 文件
简化 Maven 配置
无需 XML 配置,轻松快速地搭建Spring Web应用
开始学习SpringBoot
构建简单spring boot 项目
这里官网提供的生成器SPRING INITIALIZR 来创建简单的spring boot 项目。
1.、访问 http://start.spring.io
2、解压下载的压缩包文件demo.zip至本地目录,打开IDEA ,选择菜单>>File >>Open...
导入成功后,目录结构如下图
3、修改DemoApplication类,如下
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
public class DemoApplication {
@RequestMapping("/")
public String hello() {
return "Hello World";
} public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注:@RestController 这个注解相当于同时添加@Controller和@ResponseBody注解。
4、运行DemoApplication.java 启动项目,启动日志可以看到端口为8080
E:\ProgramFiles\java\jdk1.8.0_152\bin\java -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -javaagent:E:\ProgramFiles\IntelliJIDEA\lib\idea_rt.jar=58488:E:\ProgramFiles\IntelliJIDEA\bin -Dfile.encoding=UTF-8 -classpath E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\charsets.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\deploy.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\localedata.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\sunec.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\javaws.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\jce.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\jfr.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\jfxswt.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\jsse.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\management-agent.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\plugin.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\resources.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\rt.jar;D:\Documents\学习素材\spring\demo\target\classes;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter-web\2.0.2.RELEASE\spring-boot-starter-web-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter\2.0.2.RELEASE\spring-boot-starter-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot\2.0.2.RELEASE\spring-boot-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-autoconfigure\2.0.2.RELEASE\spring-boot-autoconfigure-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter-logging\2.0.2.RELEASE\spring-boot-starter-logging-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\Arvin\.m2\repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\Arvin\.m2\repo\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;C:\Users\Arvin\.m2\repo\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;C:\Users\Arvin\.m2\repo\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\Arvin\.m2\repo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\Arvin\.m2\repo\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter-json\2.0.2.RELEASE\spring-boot-starter-json-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\core\jackson-databind\2.9.5\jackson-databind-2.9.5.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\core\jackson-core\2.9.5\jackson-core-2.9.5.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.5\jackson-datatype-jdk8-2.9.5.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.5\jackson-datatype-jsr310-2.9.5.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.5\jackson-module-parameter-names-2.9.5.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter-tomcat\2.0.2.RELEASE\spring-boot-starter-tomcat-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\apache\tomcat\embed\tomcat-embed-core\8.5.31\tomcat-embed-core-8.5.31.jar;C:\Users\Arvin\.m2\repo\org\apache\tomcat\embed\tomcat-embed-el\8.5.31\tomcat-embed-el-8.5.31.jar;C:\Users\Arvin\.m2\repo\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.31\tomcat-embed-websocket-8.5.31.jar;C:\Users\Arvin\.m2\repo\org\hibernate\validator\hibernate-validator\6.0.9.Final\hibernate-validator-6.0.9.Final.jar;C:\Users\Arvin\.m2\repo\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\Users\Arvin\.m2\repo\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-web\5.0.6.RELEASE\spring-web-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-beans\5.0.6.RELEASE\spring-beans-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-webmvc\5.0.6.RELEASE\spring-webmvc-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-aop\5.0.6.RELEASE\spring-aop-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-context\5.0.6.RELEASE\spring-context-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-expression\5.0.6.RELEASE\spring-expression-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-core\5.0.6.RELEASE\spring-core-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-jcl\5.0.6.RELEASE\spring-jcl-5.0.6.RELEASE.jar com.example.demo.DemoApplication . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.2.RELEASE) 2018-05-13 12:56:28.095 INFO 15404 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on FX50J4200 with PID 15404 (started by Arvin in D:\Documents\学习素材\spring\demo)
2018-05-13 12:56:28.135 INFO 15404 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-05-13 12:56:28.377 INFO 15404 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@448ff1a8: startup date [Sun May 13 12:56:28 CST 2018]; root of context hierarchy
2018-05-13 12:56:31.278 INFO 15404 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-05-13 12:56:31.318 INFO 15404 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-05-13 12:56:31.318 INFO 15404 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-05-13 12:56:31.328 INFO 15404 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [E:\ProgramFiles\java\jdk1.8.0_152\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;E:\ProgramFiles\java\jdk1.8.0_152\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;E:\ProgramFiles\MySQL\MysqlServer5.7.14\bin;E:\ProgramFiles\MinGW\mingw32\bin;E:\ProgramFiles\Tools\apache-ant-1.10.1\bin;E:\ProgramFiles\Tools\apache-maven-3.5.0\bin;E:\ProgramFiles\nodejs\;E:\ProgramFiles\Tools\VisualSVN\bin;E:\ProgramFiles\Tools\gradle3.5\bin;E:\ProgramFiles\Tools\Git\Git\cmd;E:\ProgramFiles\Tools\Lua\5.1;E:\ProgramFiles\Tools\Lua\5.1\clibs;E:\ProgramFiles\Tools\Sencha\Cmd;E:\ProgramFiles\Ruby200-x64\bin;C:\Program Files\TortoiseSVN\bin;C:\Program Files\TortoiseGit\bin;E:\ProgramFiles\Tools\Brackets\command;E:\ProgramFiles\python3.6.3;E:\ProgramFiles\python3.6.3\Scripts;%ANDROID_HOME%\tools;E:\ProgramFiles\groovy\groovy-2.4.10\bin;E:\ProgramFiles\android\android-sdk-windows\platform-tools;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;E:\ProgramFiles\OracleClient\instantclient_12_2;E:\ProgramFiles\Tools\UE;C:\Users\Arvin\AppData\Local\Microsoft\WindowsApps;C:\Users\Arvin\AppData\Roaming\npm;E:\ProgramFiles\Tools\SSH Secure Shell;;.]
2018-05-13 12:56:31.462 INFO 15404 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-05-13 12:56:31.462 INFO 15404 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3143 ms
2018-05-13 12:56:31.667 INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-05-13 12:56:31.671 INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-13 12:56:31.672 INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-13 12:56:31.672 INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-13 12:56:31.672 INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-13 12:56:31.925 INFO 15404 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-13 12:56:32.235 INFO 15404 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@448ff1a8: startup date [Sun May 13 12:56:28 CST 2018]; root of context hierarchy
2018-05-13 12:56:32.345 INFO 15404 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.lang.String com.example.demo.DemoApplication.index()
2018-05-13 12:56:32.345 INFO 15404 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-05-13 12:56:32.345 INFO 15404 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-05-13 12:56:32.365 INFO 15404 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-13 12:56:32.365 INFO 15404 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-13 12:56:32.607 INFO 15404 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-05-13 12:56:32.728 INFO 15404 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-05-13 12:56:32.732 INFO 15404 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 5.951 seconds (JVM running for 8.268)
2018-05-13 12:56:48.617 INFO 15404 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-05-13 12:56:48.617 INFO 15404 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-05-13 12:56:48.633 INFO 15404 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
5、打开浏览器访问:http://localhost:8080可得结果
补充
上面讲解了可以从 http://start.spring.io 中直接构建spring-boot项目
下面讲解直接在IntelliJ IDEA中构建spring-boot项目
1、打开 IDEA ,新建项目
2、选择Spring Initializr 并在 Choose Initializr Service URL 填入 https://start.spring.io ,点击Next
3.、填入Group Artifact 等相关信息,点击Next
4、选择你需要依赖的模块,点击Next
5.、点击Finish即可完成项目的创建
spring boot入门篇,helloworld案例演示的更多相关文章
- Spring boot学习1 构建微服务:Spring boot 入门篇
Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Springboot 系列(一)Spring Boot 入门篇
注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 由于 J2EE 的开发变得笨重,繁多的配置, ...
- spring boot入门篇
Spring Boot[快速入门] Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point ...
- 构建微服务:Spring boot 入门篇
什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而 ...
- Spring boot 入门篇
详见:https://www.cnblogs.com/ityouknow/p/5662753.html 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架, ...
- Spring Boot入门篇(基于Spring Boot 2.0系列)
1:概述: Spring Boot是用来简化Spring应用的初始化开发过程. 2:特性: 创建独立的应用(jar|war形式); 需要用到spring-boot-maven-plugin插件 直接嵌 ...
- Spring Boot入门及第一个案例
一:SpringBoot是什么 springboot是对spring的缺点进行改善和优化,约定大于配置 开箱即用 没有代码生成 也无需xml 文件配置 可以修改属性值来满足需求 1) Spri ...
- spring boot 入门之 helloworld
第一步:创建一个普通的maven项目 第二步:配置springboot基础依赖配置(即配置pom和启动类) POM配置 <project xmlns="http://maven.apa ...
- Spring boot 提高篇
Spring boot 提高篇 上篇文章介绍了Spring boot初级教程:构建微服务:Spring boot 入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续 ...
随机推荐
- H5的学习之旅-H5的实体(14)
H5有些关键字比如<等等是显示不出来的,这时候,就需要用实体来表示,实体我理解就是最初的编码 代码实例 <!DOCTYPE html> <html lang="en& ...
- 用C语言绘制一条标准的余弦曲线
#include<stdio.h> #include<math.h> int main() { double y; int x,m; for(y=1;y>=-1;y-=0 ...
- Vim编译器的常用使用方法与技巧
vim操作 插入模式 命令行模式 末行模式 命令行模式 -> 插入模式 i ---> 在当前光标的前一个插入 I ---> 在行首插入 ...
- 关于Maven中打包命令(项目中打补丁的时候用到)
打jar包的方式 mvn package -Dmaven.test.skip=true mvn install -Dmaven.test.skip=true ...
- LeetCode之“数学”:Plus One
题目链接 题目要求: Given a non-negative number represented as an array of digits, plus one to the number. Th ...
- ffdshow 源代码分析 2: 位图覆盖滤镜(对话框部分Dialog)
===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...
- 苹果新的编程语言 Swift 语言进阶(十五)--协议
协议定义了适合某个特定任务或功能需要的方法.属性和其它需求的一个蓝图.协议本身不提供这些需求的实现,它只是描述了一个任务或功能实现的蓝图. 协议与java 语言中的接口定义类似,都是描述了一个实现可以 ...
- java的双缓冲技术
Java的强大特性让其在游戏编程和多媒体动画处理方面也毫不逊色.在Java游戏编程和动画编程中最常见的就是对于屏幕闪烁的处理.本文从J2SE的一个再现了屏幕闪烁的Java Appilication简单 ...
- 【39】FlexboxLayout使用介绍
FlexboxLayout介绍: Flexbox 也称为弹性盒子模型 或伸缩盒子模型,广泛用于前端开发,做过前端 web 的都知道Bootstrap 中有一套强大的 CSS Grid网格样式.Boot ...
- break和continue的简单介绍
1.break break 用于完全结束一个循环,跳出循环体!不管是哪种循环,如果在程序中遇到Break,系统将完全结束该循环,开始执行循环之后的代码: public class TestBreak ...