springboot @Controller和@RestController的區(qū)別及應(yīng)用詳解
@Controller和@RestController的區(qū)別及應(yīng)用
@Controller和@RestController區(qū)別
在springboot開(kāi)發(fā)中控制層使用注解@Controller時(shí),加有@GetMapping(@PostMapping或@RequestMapping)注解的方法返回值對(duì)應(yīng)的是一個(gè)視圖,而使用@RestController返回值對(duì)應(yīng)的是json數(shù)據(jù),而@Controller+@ResponseBody的作用相當(dāng)于@RestController。
@Controller的應(yīng)用
先在application.properties配置文件中配置
spring.mvc.view.prefix=classpath:/templates/ spring.mvc.view.suffix=.html
然后在控制層CustomerController類(lèi)的代碼為
@Controller public class CustomerController { @Resource CustomerServiceI customerServiceI; @GetMapping("/") public String index() { return "redirect:/list"; } @GetMapping("/list") public String list(Model model) { List<Customer> users = customerServiceI.getUserList(); model.addAttribute("users",users); return "list"; } }
啟動(dòng)程序后在瀏覽器輸入localhost:8080/list訪(fǎng)問(wèn)頁(yè)面即為templates文件夾下的list.html
@RestController的應(yīng)用
控制層CustomerController類(lèi)的代碼為
@RestController public class CustomerController { @Resource CustomerServiceI customerServiceI; @GetMapping("/") public String index() { return "redirect:/list"; } @GetMapping("/list") public List<Customer> list(Model model) { List<Customer> users = customerServiceI.getUserList(); model.addAttribute("users",users); return users; } }
啟動(dòng)程序后在瀏覽器輸入localhost:8080/list訪(fǎng)問(wèn)效果如下
@Controller和@RestController區(qū)別的小坑
這兩個(gè)的區(qū)別其實(shí)是個(gè)很簡(jiǎn)單的問(wèn)題,但是對(duì)于初學(xué)者可能遇到了會(huì)掉坑里。
@RestController注解相當(dāng)于@ResponseBody + @Controller合在一起的作用。
1.如果注解Controller使用@RestController
則Controller中的方法無(wú)法返回jsp頁(yè)面,或者h(yuǎn)tml,配置的視圖解析器 InternalResourceViewResolver不起作用,返回的內(nèi)容就是Return 里的內(nèi)容。
代碼如圖:
結(jié)果如圖:
2.如果需要返回到指定頁(yè)面(jsp/html)
則需要用 @Controller配合視圖解析器InternalResourceViewResolver才行。
代碼如圖:
結(jié)果如圖:
如果需要返回JSON,XML或自定義mediaType內(nèi)容到頁(yè)面,則需要在對(duì)應(yīng)的方法上加上@ResponseBody注解。
代碼如圖:
結(jié)果如圖:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Spring注解@RestControllerAdvice原理解析
- SpringBoot http請(qǐng)求注解@RestController原理解析
- SpringBoot的@RestControllerAdvice作用詳解
- SpringBoot常用注解@RestControllerAdvice詳解
- Spring中@RestControllerAdvice注解的使用詳解
- Spring中的@RestController注解詳細(xì)解析
- Spring @RestController注解組合實(shí)現(xiàn)方法解析
- springboot中@RestController注解實(shí)現(xiàn)
- Spring中@RestController注解的使用實(shí)現(xiàn)
相關(guān)文章
SpringBoot后端接口的實(shí)現(xiàn)(看這一篇就夠了)
這篇文章主要介紹了SpringBoot后端接口的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Sharding-jdbc報(bào)錯(cuò):Missing the data source
在使用MyBatis-plus進(jìn)行數(shù)據(jù)操作時(shí),新增Order實(shí)體屬性后,出現(xiàn)了數(shù)據(jù)源缺失的提示錯(cuò)誤,原因是因?yàn)閡serId屬性值使用了隨機(jī)函數(shù)生成的Long值,這與sharding-jdbc的路由規(guī)則計(jì)算不匹配,導(dǎo)致無(wú)法找到正確的數(shù)據(jù)源,通過(guò)調(diào)整userId生成邏輯2024-11-11C++/java 繼承類(lèi)的多態(tài)詳解及實(shí)例代碼
這篇文章主要介紹了C++/java 繼承類(lèi)的多態(tài)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Java實(shí)現(xiàn)直接插入排序與折半插入排序的示例詳解
這篇文章主要為大家詳細(xì)介紹了插入排序中兩個(gè)常見(jiàn)的排序:直接插入排序與折半插入排序。本文用Java語(yǔ)言實(shí)現(xiàn)了這兩個(gè)排序算法,感興趣的可以學(xué)習(xí)一下2022-06-06eclipse下整合springboot和mybatis的方法步驟
這篇文章主要介紹了eclipse下整合springboot和mybatis的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03