打开 foodie-dev 的 pom 文件,添加 Spring Boot 依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.imooc</groupId><artifactId>foodie-dev</artifactId><version>1.0-SNAPSHOT</version><modules><module>foodie-dev-common</module><module>foodie-dev-pojo</module><module>foodie-dev-mapper</module><module>foodie-dev-service</module><module>foodie-dev-api</module></modules><!--1. 聚合工程里可以分为顶级项目(顶级工程、父工程)与子工程,这两者的关系其实就是父子继承的关系。子工程在 maven 里称之为模块(module),模块之间是平级,是可以相互依赖的。2. 子模块可以使用顶级工程里所有的资源(依赖),子模块之间如果要使用资源,必须构建依赖(构建关系)。3. 一个顶级工程是可以由多个不同的子工程共同组合而成。--><packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.encoding>UTF-8</maven.compiler.encoding><java.version>14</java.version><maven.compiler.source>14</maven.compiler.source><maven.compiler.target>14</maven.compiler.target></properties><!--Spring 相关模块不需要指定版本号,版本已由父级管理--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--解析 xml 等其他 spring 配置文件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies></project>
在 foodie-dev-api 的 resources 文件夹下创建配置文件
在 foodie-dev-api 创建 Application.java 文件,Spring Boot 启动类
在 foodie-dev-api 模块中的 com.imooc.controller 包下面创建 HelloController.java 文件
package com.imooc.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 92578
* @since 1.0
*/
@RestController
public class HelloController {
@GetMapping("/hello")
public Object hello() {
return "Hello World~";
}
}
启动项目,运行成功
打开浏览器,访问 http://localhost:8080/hello
