1.添加依赖
在pom.xml 文件中添加一层路由的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web RESTFUL 路由层-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2. 创建路由层的类
添加package包并创建类CoreController.java
添加注解@RestController告诉容器这是路由层
添加方法并注解说明请求的地址是/hello
package com.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CoreController {
@RequestMapping("/hello")
public String hello() {
return "-hello world-";
}
}
3.启动
启动DemoApplication的mian方法
端口是8080
这个时候访问:http://localhost:8080/hello
成功返回“hello world”