问题
使用spring boot ,写rest demo时,使用的是 @Controller,出现 404,但加入日志,发现,已经进入方法体。如下图
此类问题,已经超脱原来的认知,作为程序猿,404的异常,是没有找到对应的资源路径才会出现
问题解决
标签@Controller调整为@RestController,访问正常。或是 在@Controller 添加标签@ResponseBody也可以正常返回数据。
在spring rest 中 @RestController 可以理解为等于 @Controller @ResponseBody。但@Controller 可以与模板引擎使用
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
添加模板文件
文件内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>this is index Controller</h1> </body> </html>
访问效果
总结
使用@Controller 出现404时,应该异常的含义是,无法找到对应的模板或是 view。