1. 程式人生 > >異常:Error resolving template "xxx", template might not exist or might not be accessible...解決辦法

異常:Error resolving template "xxx", template might not exist or might not be accessible...解決辦法

控制臺 返回值 resp body 網上 資料 必須 exist https

簡單表述:控制臺出現了這個異常:Error resolving template "xxx", template might not exist or might not be accessible by any of the configured Template Resolvers

解決辦法:在報出這個異常的方法上添加註解@ResponseBody

講一下個人理解:這個註解就是用來區別 方法的返回值字符串 和 視圖解析器解析的頁面名字字符串 的沖突的,舉個例子:方法A返回的字符串success,如果和ajax的回調函數裏的msg匹配,就彈出一個“執行成功”,因為success只是普通的字符串,所以要在方法上邊加上@ResponseBody。 方法B的返回值是“/contextList”,而這個就是一個頁面contextList.jsp或者是contextList.html(具體是什麽看配置,反正是個頁面)。

網上找的資料(博客鏈接:https://blog.csdn.net/Ocean_tu/article/details/80989584):

controller層加註解@Controller 和@RestController都可以在前端調通接口,但是二者的區別在於,當用前者的時候在方法上必須添加註解@ResponseBody,如果不添加@ResponseBody,就會報上面錯誤,因為當使用@Controller 註解時,spring默認方法返回的是view對象(頁面)。而加上@ResponseBody,則方法返回的就是具體對象了。@RestController的作用就相當於@Controller+@ResponseBody的結合體

異常:Error resolving template "xxx", template might not exist or might not be accessible...解決辦法