查看“Spring Boot-HelloWorld”的源代码
←
Spring Boot-HelloWorld
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
=== Spring Boot 2 入门 === ==== 系统要求 ==== Java 8 & 兼容 java 14 Maven 3.3+ idea 2019.12 ==== maven 设置 ==== <syntaxhighlight lang="xml"> <mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors> <profiles> <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile> </profiles> </syntaxhighlight>https://www.bilibili.com/video/BV19K4y1L7MT?p=5 === Java代码 === ==== MainApplication.java ==== <syntaxhighlight lang="java"> package io.github.jihch.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 主程序类 * @SpringBootApplication:这是一个 Spring Boot 应用 */ @SpringBootApplication public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } } </syntaxhighlight> ==== HelloController.java ==== <syntaxhighlight lang="java"> package io.github.jihch.boot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String handle01() { return "Hello, Spring Boot 2!"; } } </syntaxhighlight>测试URL:http://localhost:8080/hello === Application.yml === ==== 修改 tomcat 监听端口 ==== <syntaxhighlight lang="yaml"> server: port: 8888 </syntaxhighlight> ==== 官方文档配置属性说明 ==== https://docs.spring.io/spring-boot/docs/2.7.8/reference/html/application-properties.html#appendix.application-properties === 创建可执行文件 === https://docs.spring.io/spring-boot/docs/2.7.8/reference/html/getting-started.html#getting-started.first-application.executable-jar ==== 在 pom.xml 中引入插件 ==== <syntaxhighlight lang="xml"> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </syntaxhighlight> ==== 清理、打包 ==== [[文件:IDEA 中调用 Maven 的生命周期阶段1.png|无|缩略图]] 在 target 目录下得到可执行文件 boot-01-helloworld-1.0-SNAPSHOT.jar ==== 在命令行中独立运行 ==== <syntaxhighlight lang="powershell"> E:\record\2023\1\28\boot-01-helloworld\target>java -jar boot-01-helloworld-1.0-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.4.RELEASE) 2023-02-01 14:35:45.878 INFO 7620 --- [ main] io.github.jihch.boot.MainApplication : Starting MainApplication v1.0-SNAPSHOT on SK-20210414CLPG with PID 7620 (E:\record\2023\1\28\boot-01-helloworld\target\boot-01-helloworld-1.0-SNAPSHOT.jar started by Administrator in E:\record\2023\1\28\boot-01-helloworld\target) 2023-02-01 14:35:45.880 INFO 7620 --- [ main] io.github.jihch.boot.MainApplication : No active profile set, falling back to default profiles: default 2023-02-01 14:35:47.311 INFO 7620 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8888 (http) 2023-02-01 14:35:47.324 INFO 7620 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-02-01 14:35:47.325 INFO 7620 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38] 2023-02-01 14:35:47.395 INFO 7620 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2023-02-01 14:35:47.395 INFO 7620 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1446 ms 2023-02-01 14:35:47.542 INFO 7620 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2023-02-01 14:35:47.719 INFO 7620 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path '' 2023-02-01 14:35:47.728 INFO 7620 --- [ main] io.github.jihch.boot.MainApplication : Started MainApplication in 2.511 seconds (JVM running for 3.704) </syntaxhighlight> === 注意事项 === windows 中,在命令行中运行可执行文件时,如果遇到鼠标点击就会暂停运行的情况,可能是因为启用了“快速编辑模式” [[文件:Windows 命令行快速编辑模式.png|无|缩略图|591x591像素]] 取消掉勾选就可以了
返回至
Spring Boot-HelloWorld
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息