Springboot入门程序
springboot简化之前的很多xml文件,不需要大量配置xml文件,没有很多xml文件是多么的愉快
下面进入正题,演示简单的入门程序,虽然简单,但足以振奋初学者们
我新建的工程名叫hello,大家可以参考
一、建立一个war工程,导入正确的依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | < 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.springboot.demo</ groupId > < artifactId >hello</ artifactId > < version >0.0.1-SNAPSHOT</ version > < packaging >war</ packaging > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.3.4.RELEASE</ version > </ parent > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-compiler-plugin</ artifactId > < version >2.0.2</ version > < configuration > < source >1.8</ source > < target >1.8</ target > </ configuration > </ plugin > </ plugins > </ build > </ project > |
二、建包建java文件
package com.example; 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 String home() { return "Hello,spring boot!"; } public static void main(String[] args) { SpringApplication.run(HelloController.class, args); } }
三、在HelloController里面执行main方法
出现如下图所示,表示启动成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | ' _| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1. 3.4 .RELEASE) 2017 - 12 - 24 18 : 45 : 26.059 INFO 13340 --- [ main] com.example.HelloController : Starting HelloController on LAPTOP-AP8KJEQL with PID 13340 (E:\Demo\workspace\hello\target\classes started by eluzhu in E:\Demo\workspace\hello) 2017 - 12 - 24 18 : 45 : 26.061 INFO 13340 --- [ main] com.example.HelloController : No active profile set, falling back to default profiles: default 2017 - 12 - 24 18 : 45 : 26.086 INFO 13340 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext @24b1d79b : startup date [Sun Dec 24 18 : 45 : 26 CST 2017 ]; root of context hierarchy 2017 - 12 - 24 18 : 45 : 27.110 INFO 13340 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017 - 12 - 24 18 : 45 : 27.121 INFO 13340 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2017 - 12 - 24 18 : 45 : 27.122 INFO 13340 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/ 8.0 . 33 2017 - 12 - 24 18 : 45 : 27.241 INFO 13340 --- [ost-startStop- 1 ] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017 - 12 - 24 18 : 45 : 27.242 INFO 13340 --- [ost-startStop- 1 ] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1158 ms 2017 - 12 - 24 18 : 45 : 27.486 INFO 13340 --- [ost-startStop- 1 ] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017 - 12 - 24 18 : 45 : 27.490 INFO 13340 --- [ost-startStop- 1 ] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [ /*] 2017-12-24 18:45:27.490 INFO 13340 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2017-12-24 18:45:27.490 INFO 13340 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2017-12-24 18:45:27.490 INFO 13340 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2017-12-24 18:45:27.679 INFO 13340 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@24b1d79b: startup date [Sun Dec 24 18:45:26 CST 2017]; root of context hierarchy 2017-12-24 18:45:27.728 INFO 13340 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto java.lang.String com.example.HelloController.home() 2017-12-24 18:45:27.731 INFO 13340 --- [ 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-12-24 18:45:27.731 INFO 13340 --- [ 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-12-24 18:45:27.751 INFO 13340 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-12-24 18:45:27.751 INFO 13340 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-12-24 18:45:27.775 INFO 13340 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/ favicon.ico] onto handler of type [ class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017 - 12 - 24 18 : 45 : 27.839 INFO 13340 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017 - 12 - 24 18 : 45 : 27.901 INFO 13340 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017 - 12 - 24 18 : 45 : 27.904 INFO 13340 --- [ main] com.example.HelloController : Started HelloController in 2.094 seconds (JVM running for 2.468 ) |
四、第三步执行成功后,在浏览器输入localhost:8080/hello就可以看到hello,springboot这句话了,就表示入门程序成功了
最后说明:springboot远远不止这么点功能,还有很多了,一起探索吧
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述