链接 🔗

新建一个空的 Maven 项目

可以使用 Eclipse / Intellij IDEA

编辑 pom.xml 文件

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  4. <java.version>11</java.version>
  5. </properties>
  6. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.2.1.RELEASE</version>
  11. </parent>
  12. <dependencies>
  13. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-web</artifactId>
  17. <version>${project.parent.version}</version>
  18. </dependency>
  19. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <version>${project.parent.version}</version>
  24. <scope>test</scope>
  25. </dependency>
  26. </dependencies>
  27. <build>
  28. <plugins>
  29. <plugin>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-maven-plugin</artifactId>
  32. </plugin>
  33. </plugins>
  34. </build>
  35. <repositories>
  36. <repository>
  37. <id>spring-releases</id>
  38. <url>https://repo.spring.io/libs-release</url>
  39. </repository>
  40. </repositories>

新建一个 Package

src/main/java下新建com.anydong.example.springboot Package

添加启动类 Application.java

  1. package com.anydong.example.springboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * Application
  6. *
  7. * @author Where
  8. * @date 2019-01-31
  9. */
  10. @SpringBootApplication
  11. public class Application {
  12. public static void main(String[] args) {
  13. SpringApplication.run(Application.class,args);
  14. }
  15. }

创建 IndexController.java 控制器文件

  1. package com.anydong.example.springboot.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestMethod;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * IndexController
  7. *
  8. * @author Where
  9. * @date 2019-01-31
  10. */
  11. @RestController
  12. @RequestMapping(produces = "application/json; charset=utf-8")
  13. public class IndexController {
  14. @RequestMapping(value = "/ping", method = RequestMethod.GET)
  15. public String ping() {
  16. return "pong";
  17. }
  18. }

运行 Application.java 项目创建成功