SpringBoot创建项目并启动

您所在的位置:网站首页 创建一个springboot项目,完成序列号生成接口 SpringBoot创建项目并启动

SpringBoot创建项目并启动

2024-07-13 16:57| 来源: 网络整理| 查看: 265

案例创建一个新的项目并测试启动

此时项目结构如下,可能pom.xml文件会报错

这是因为spring boot 2.1.8的maven插件与eclipse不兼容。这时需要在pom.xml里面添加一行,指定maven插件的版本,如下:

1.8 3.1.1

这时更新项目即可(Maven-Update Project),没有报错信息

整个的pom.xml文件如下:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE com.example cn.tedu 0.0.1-SNAPSHOT spring_demo Demo project for Spring Boot 1.8 3.1.1 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

此时整个项目结构如下:

启动springBoot:打开SpringDemoApplication.java文件,Run As --Spring Boot App,显示如下页面:

. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.8.RELEASE) 2019-09-18 11:49:42.538 INFO 18784 --- [ main] cn.tedu.SpringDemoApplication : Starting SpringDemoApplication on LAPTOP-JLA8JBR3 with PID 18784 (E:\workspace\spring_demo\target\classes started by wq in E:\workspace\spring_demo) 2019-09-18 11:49:42.541 INFO 18784 --- [ main] cn.tedu.SpringDemoApplication : No active profile set, falling back to default profiles: default 2019-09-18 11:49:43.709 INFO 18784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-09-18 11:49:43.757 INFO 18784 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-09-18 11:49:43.758 INFO 18784 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24] 2019-09-18 11:49:43.943 INFO 18784 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-09-18 11:49:43.943 INFO 18784 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1334 ms 2019-09-18 11:49:44.220 INFO 18784 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-09-18 11:49:44.427 INFO 18784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-09-18 11:49:44.431 INFO 18784 --- [ main] cn.tedu.SpringDemoApplication : Started SpringDemoApplication in 2.214 seconds (JVM running for 3.112)

可以看到,端口号为8080,

这是写一个测试HelloWord,注意:

其余自己写的都必须在SpringDemoApplication.java文件的子包下面,也就是说,其他的包,需要是application文件所在包的子包

测试类如下:

package cn.tedu.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping("/hello") public String hello(){ return "hello World"; } }

包结构如下:

此时先关闭之前的启动,再次启动,否则会报出出端口被占用的问题,再次Run As --Spring Boot App

此时浏览器显示:

这时一个SpringBoot项目就搭建好了



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3