1、系统要求

  • Java 8 & 兼容java14 .
  • Maven 3.3+

1.1、maven设置

目录:…maven\lib\maven3\conf\settings.xml
标签中加入

  1. <!--阿里镜像-->
  2. <mirrors>
  3. <mirror>
  4. <id>nexus-aliyun</id>
  5. <mirrorOf>central</mirrorOf>
  6. <name>Nexus aliyun</name>
  7. <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  8. </mirror>
  9. </mirrors>
  10. <!--设置maven项目用1.8JDK编译-->
  11. <profiles>
  12. <profile>
  13. <id>jdk-1.8</id>
  14. <activation>
  15. <activeByDefault>true</activeByDefault>
  16. <jdk>1.8</jdk>
  17. </activation>
  18. <properties>
  19. <maven.compiler.source>1.8</maven.compiler.source>
  20. <maven.compiler.target>1.8</maven.compiler.target>
  21. <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  22. </properties>
  23. </profile>
  24. </profiles>

2、HelloWorld

需求:浏览发送/hello请求,响应 Hello,Spring Boot 2

2.1、创建maven工程

2.2、引入依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.3.4.RELEASE</version>
  5. </parent>
  6. <dependencies>
  7. <!--web 依赖-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. </dependencies>

2.3、创建主程序

  1. /**
  2. * 主程序类
  3. * @SpringBootApplication:这是一个SpringBoot应用
  4. */
  5. @SpringBootApplication
  6. public class MainApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(MainApplication.class,args);
  9. }
  10. }

2.4、编写业务

  1. @RestController
  2. public class HelloController {
  3. @RequestMapping("/hello")
  4. public String handle01(){
  5. return "Hello, Spring Boot 2!";
  6. }
  7. }

2.5、测试

直接运行main方法

2.6、简化配置

application.properties

  1. server.port=8888

2.7、简化部署

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. </build>

注意点:

  • 取消掉cmd的快速编辑模式(避免出现选择断点情况)
  • 当前目录打开cmd
  • 启动命令: java -jar 包名.jar
  • 使用maven打包:1.clean 2.package

    image.png