“Spring Boot-HelloWorld”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (→Java代码) |
Jihongchang(讨论 | 贡献) (→注意事项) |
||
(未显示同一用户的4个中间版本) | |||
第82行: | 第82行: | ||
} | } | ||
+ | </syntaxhighlight>测试URL:http://localhost:8080/hello | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === Application.yml === | ||
+ | |||
+ | ==== 修改 tomcat 监听端口 ==== | ||
+ | <syntaxhighlight lang="yaml"> | ||
+ | server: | ||
+ | port: 8888 | ||
</syntaxhighlight> | </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像素]] | ||
+ | 取消掉勾选就可以了 |
2023年2月1日 (三) 06:46的最新版本
Spring Boot 2 入门
系统要求
Java 8 & 兼容 java 14
Maven 3.3+
idea 2019.12
maven 设置
<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>
https://www.bilibili.com/video/BV19K4y1L7MT?p=5
Java代码
MainApplication.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);
}
}
HelloController.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!";
}
}
测试URL:http://localhost:8080/hello
Application.yml
修改 tomcat 监听端口
server:
port: 8888
官方文档配置属性说明
创建可执行文件
在 pom.xml 中引入插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
清理、打包
在 target 目录下得到可执行文件 boot-01-helloworld-1.0-SNAPSHOT.jar
在命令行中独立运行
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)
注意事项
windows 中,在命令行中运行可执行文件时,如果遇到鼠标点击就会暂停运行的情况,可能是因为启用了“快速编辑模式”
取消掉勾选就可以了