ajax调用controller层方法 404_spring boot 中 @RestController and @Controller 有什么不同?...

问题

使用spring boot ,写rest demo时,使用的是 @Controller,出现 404,但加入日志,发现,已经进入方法体。如下图

33748aff176a89b470dec554d68f9957.png

异常 404

f4c19debfe0fd05f3381cd18a4a50d1e.png

进入方法体

此类问题,已经超脱原来的认知,作为程序猿,404的异常,是没有找到对应的资源路径才会出现

21a114bacad3c96fca2b3e5513bf299f.png

问题解决

标签@Controller调整为@RestController,访问正常。或是 在@Controller 添加标签@ResponseBody也可以正常返回数据。

在spring rest 中 @RestController 可以理解为等于 @Controller @ResponseBody。但@Controller 可以与模板引擎使用

添加依赖

org.springframework.boot spring-boot-starter-thymeleaf 

添加模板文件

107064784a8b6b8c8a686bf5bd9e21f1.png

结构

文件内容

 Title

this is index Controller

访问效果

107057b234bdd8e9865bb16ea3024f51.png

总结

使用@Controller 出现404时,应该异常的含义是,无法找到对应的模板或是 view。

7627ee382084878aeafe60dba0709841.png

spring mvc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值