1. 程式人生 > >template might not exist or may not be accessible by any of the configured Template Resolvers檢視解析錯誤

template might not exist or may not be accessible by any of the configured Template Resolvers檢視解析錯誤

樓主用的是springboot+hibernate+thymeleaf。不知道這個選型是否和問題有相關性,姑且列在這裡,可以參考。

一開始遇到這個問題的時候上網查過,看到大多提到什麼取消斜槓的方法,並沒有解決。自己搗鼓下來發現是遺漏了@Response註解


將@Controller改為@RestController(也相當於改為@Responsebody和@Controller合併在一起)就可以解決

原因在於這裡teacherRepository.save(teacher)的返回值是一個json字串。
而@Responsebody的作用是:將Controller的方法返回的物件,通過適當的HttpMessageConverter轉換為指定格式後,寫入到Response物件的body資料區。也就是@Responsebody適用於返回不是html頁面,而是其他某種格式的資料時(如json、xml等)