在项目下建立controller文件夹,在其中编写 HelloController
package com.example.demo2.springbootmybatis.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@Controller //Controller注解表示这个类注册为Controller组件@RequestMapping("/hello")public class HelloController {@GetMapping("")public String sayHello(){return "Hello,Springboot";}}

运行项目,打开Postman测试
输入对应的路径: [http://localhost:8080/hello](http://localhost:8080/hello)
但是发现提示 404 Not Found
问题其实出现在我们当时注册Controller组件的时候使用的注解 @Controller@Controller 注解不能将结果写入到  HTTP response body 中,因而无法正常显示。
因此我们还需要使用另外一个注解 @ResponseBody来将我们的内容写入到返回体中,如下所示
重启我们的应用,再次测试
发现已经可以正常请求
除此之外,我们还可以使用另外一种注册Controller的注解来解决这个问题
即 @RestController
发现也可以正常使用
参考:@RestController 和 @Controller 的区别
@RestController 和 @Controller 的区别倪默遥-CSDN博客@restcontroller和@controller的区别
