该文章是Java入门系列的第六章:配置路由
添加配置路由的文件
首先我们在 org 的 example 文件下新建一个 Java 类,命名为 Controller,我们在这个文件中进行路由的配置
Controller文件代码如下:
其中,注解@RestController
表示这是一个 Control 类,@GetMapping
表示对get请求要进行怎么样的操作,我们的代码中是访问/test
接口,返回“test”
package org.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@GetMapping("/test")
public String test() {
return "test";
}
}
我们重新运行项目,打开http://localhost:8080/test ,可以看到页面上已经返回了“test”
对于post请求,我们就可以在上面的代码使用@PostMapping
,这样我们就写好了一个最简单的接口
写在最后
以上就是我们在项目中配置路由的过程