javascript
Spring MVC:带有CNVR卷的REST应用程序。 2
在上一篇文章中,我快速概述了帶有CNVR的Spring MVC REST項目的設置環境。 在這一部分中,我可以直接關注控制器和REST服務的演示。 通常,我將做一個簡短的介紹,然后我將介紹控制器方法并解釋所有關鍵時刻。
由于我將進一步討論REST服務,因此我需要說一些有關REST基本概念的句子。 您可能之前聽說過提供API來使用其功能的站點。 借助REST或SOAP,這成為可能,但是在本文中,我將討論REST。
例如,您想為大學圖書館開發一個可以與學生和書籍一起使用的應用程序。 您可以使用REST實現所有控制器。 該決定將使您的應用程序打開,以便與可以使用該應用程序API的其他應用程序進行協作。 有關REST功能的更多信息,您需要訪問特殊站點 。
Spring MVC REST控制器
Smartphone應用程序面向HTTP客戶端(例如瀏覽器)和JSON客戶端。 JSON格式可由各種類型的客戶端使用,但現在不再重要。
讓我們考慮整個控制器代碼:
@Controller @RequestMapping(value="/smartphones") public class SmartphoneController {@Autowiredprivate SmartphoneService smartphoneService;@RequestMapping(value="/create", method=RequestMethod.GET)public ModelAndView createSmartphonePage() {ModelAndView mav = new ModelAndView("phones/new-phone");mav.addObject("sPhone", new Smartphone());return mav;}@RequestMapping(value="/create", method=RequestMethod.POST)public ModelAndView createSmartphone(@ModelAttribute Smartphone smartphone,final RedirectAttributes attributes) {ModelAndView mav = new ModelAndView("redirect:/index.html");createSmartphone(smartphone);attributes.addFlashAttribute("msg", "New Smartphone "+smartphone+" was successfully created.");return mav;}@RequestMapping(value="/create", method=RequestMethod.POST, produces = "application/json", consumes = "application/json")@ResponseBodypublic Smartphone createSmartphone(@RequestBody Smartphone smartphone) {return smartphoneService.create(smartphone);}@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)public ModelAndView editSmartphonePage(@PathVariable int id) {ModelAndView mav = new ModelAndView("phones/edit-phone");Smartphone smartphone = smartphoneService.get(id);mav.addObject("sPhone", smartphone);return mav;}@RequestMapping(value="/edit/{id}", method=RequestMethod.PUT, produces = "application/json", consumes = "application/json")@ResponseBodypublic Smartphone editSmartphone(@PathVariable int id, @RequestBody Smartphone smartphone) {smartphone.setId(id);return smartphoneService.update(smartphone);}@RequestMapping(value="/edit/{id}", method=RequestMethod.PUT)public ModelAndView editSmartphone(@PathVariable int id,@ModelAttribute Smartphone smartphone,final RedirectAttributes attributes) {ModelAndView mav = new ModelAndView("redirect:/index.html");editSmartphone(id, smartphone);attributes.addFlashAttribute("msg", "The Smartphone "+smartphone+" was successfully updated.");return mav;}@RequestMapping(value="/delete/{id}", method=RequestMethod.DELETE, produces = "application/json", consumes = "application/json")@ResponseBodypublic Smartphone deleteSmartphone(@PathVariable int id) {return smartphoneService.delete(id);}@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)public ModelAndView deleteSmartphone(@PathVariable int id,final RedirectAttributes attributes) {ModelAndView mav = new ModelAndView("redirect:/index.html");Smartphone deletedSphone = deleteSmartphone(id);attributes.addFlashAttribute("msg", "The Smartphone "+deletedSphone+" was successfully deleted.");return mav;}@RequestMapping(value="", method=RequestMethod.GET,produces = "application/json", consumes = "application/json")@ResponseBodypublic List< Smartphone > allPhones() {return smartphoneService.getAll();}@RequestMapping(value="", method=RequestMethod.GET)public ModelAndView allPhonesPage() {ModelAndView mav = new ModelAndView("phones/all-phones");List< Smartphone > smartphones = new ArrayList< Smartphone >();smartphones.addAll(allPhones());mav.addObject("smartphones", smartphones);return mav;}}Smartphone控制器確實很冗長,并且有很多方法。 在控制器的開頭,您可以看到自動連線的SmartphoneService 。 反過來,SmartphoneService具有五種方法:
- 公共智能手機創建(Smartphone sp);
- 公用智能手機get(整數id);
- 公共列表<Smartphone> getAll();
- 公共智能手機更新(Smartphone sp)拋出SmartphoneNotFoundException;
- 公共智能手機delete(Integer id)拋出SmartphoneNotFoundException;
控制器中的每種方法都對應于服務的特定方法。 因此,讓我們在以下部分中檢查這種對應關系。
REST:創建
以下代碼段負責創建新的智能手機實體:
...@RequestMapping(value="/create", method=RequestMethod.POST)public ModelAndView createSmartphone(@ModelAttribute Smartphone smartphone,final RedirectAttributes attributes) {ModelAndView mav = new ModelAndView("redirect:/index.html");createSmartphone(smartphone);attributes.addFlashAttribute("msg", "New Smartphone "+smartphone+" was successfully created.");return mav;}@RequestMapping(value="/create", method=RequestMethod.POST, produces = "application/json", consumes = "application/json")@ResponseBodypublic Smartphone createSmartphone(@RequestBody Smartphone smartphone) {return smartphoneService.create(smartphone);} ...第一種方法是簡單的Spring MVC控制器。 我在以前的文章中多次解釋了如何使用Spring MVC控制器。 但是您會注意到該方法是不尋常的,因為它包含第二個方法的調用。 第二種方法是具有標準REST注釋的REST方法: @ResponseBody和@RequestBody 。
當您將包含有關新智能手機的數據的表單提交到“ ../smartphones/create.html”進行處理時, 內容協商視圖解析器將確定您需要接收html頁面。 如果您將URL稱為“ ../smartphones/create.json”,則會返回JSON文檔。 因為我在WebAppConfig中指定CNVR需要根據URL sufix做出決定。
您可以問:如果我們仍然需要為同一操作創建幾種方法,那么使用CNVR的原因是什么? 讓我們假設智能手機應用程序必須支持2種額外的內容類型:XML和PDF。 在這種情況下,CNVR將使我們的生活更輕松,并且我們不會開發其他方法,只需在WebAppConfig中添加適當的視圖解析器即可 。 如果我們開始在應用程序中使用AJAX,這種情況將變得非常理想。 這意味著我們可以消除返回ModelAndView對象的方法。
REST:獲取所有記錄
在上一段中,我對CNVR原則進行了詳細的概述。 因此,現在我將發布與相應操作相對應的具體代碼段。
...@RequestMapping(value="", method=RequestMethod.GET,produces = "application/json", consumes = "application/json")@ResponseBodypublic List< Smartphone > allPhones() {return smartphoneService.getAll();}@RequestMapping(value="", method=RequestMethod.GET)public ModelAndView allPhonesPage() {ModelAndView mav = new ModelAndView("phones/all-phones");List< Smartphone > smartphones = new ArrayList< Smartphone >();smartphones.addAll(allPhones());mav.addObject("smartphones", smartphones);return mav;} ...這些方法負責檢索智能手機列表。
REST:更新
以下是執行現有智能手機更新的方法。
...@RequestMapping(value="/edit/{id}", method=RequestMethod.PUT, produces = "application/json", consumes = "application/json")@ResponseBodypublic Smartphone editSmartphone(@PathVariable int id, @RequestBody Smartphone smartphone) {smartphone.setId(id);return smartphoneService.update(smartphone);}@RequestMapping(value="/edit/{id}", method=RequestMethod.PUT)public ModelAndView editSmartphone(@PathVariable int id,@ModelAttribute Smartphone smartphone,final RedirectAttributes attributes) {ModelAndView mav = new ModelAndView("redirect:/index.html");editSmartphone(id, smartphone);attributes.addFlashAttribute("msg", "The Smartphone "+smartphone+" was successfully updated.");return mav;} ...休息:刪除
以下是執行刪除現有智能手機的方法。
...@RequestMapping(value="/delete/{id}", method=RequestMethod.DELETE, produces = "application/json", consumes = "application/json")@ResponseBodypublic Smartphone deleteSmartphone(@PathVariable int id) {return smartphoneService.delete(id);}@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)public ModelAndView deleteSmartphone(@PathVariable int id,final RedirectAttributes attributes) {ModelAndView mav = new ModelAndView("redirect:/index.html");Smartphone deletedSphone = deleteSmartphone(id);attributes.addFlashAttribute("msg", "The Smartphone "+deletedSphone+" was successfully deleted.");return mav;} ...摘要
我希望這部分對您來說很清楚。 無疑,您需要具備Spring和REST的一些基本知識才能完全理解本文。 不要忽略我在文章中提供的鏈接以獲取更多信息。 在第三部分中,我將演示此應用程序的工作方式。
翻譯自: https://www.javacodegeeks.com/2013/07/spring-mvc-rest-application-with-cnvr-vol-2.html
總結
以上是生活随笔為你收集整理的Spring MVC:带有CNVR卷的REST应用程序。 2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓事件分发流程(安卓事件分发)
- 下一篇: Java 8的新增功能(第二部分–可能会