在上一篇文章中,我快速概述了带有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的一些基本知识才能完全理解本文。 不要忽略我在文章中提供的链接以获取更多信息。 在第三部分中,我将演示此应用程序的工作方式。

参考: Spring MVC:具有CNVR卷的REST应用程序。 2来自我们的JCG合作伙伴 Alexey Zvolinskiy,在Fruzenshtein的笔记博客中。

翻译自: https://www.javacodegeeks.com/2013/07/spring-mvc-rest-application-with-cnvr-vol-2.html

Spring MVC:带有CNVR卷的REST应用程序。 2相关推荐

  1. Spring MVC:带有CNVR卷的REST应用程序。 1个

    不久前,我阅读了Paul Chapman撰写的有关内容协商视图解析器 (CNVR)的文章. Spring Framework Blog上的那篇文章启发了我研究这个框架的领域. 因此,我开发了一个基于S ...

  2. Spring MVC:带有CNVR卷的REST应用程序。 3

    这是带有CNVR的Spring MVC REST教程的最后一部分. 在这里,我将演示所有这些东西如何工作,这是我在前两部分中开发的. 对于每种类型的CRUD操作,这将分为四个部分:CREATE,REA ...

  3. java mvc 小程序_[Java教程]Spring MVC 的环境搭建和入门小程序

    [Java教程]Spring MVC 的环境搭建和入门小程序 0 2017-02-17 00:00:16 1.1.下载spring框架包. 1.1.1百度搜索Spring Framework. 进入s ...

  4. Spring、Spring MVC、Spring Boot三者的关系还傻傻分不清楚?

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 在本文中,你将获取到有关于Spring,Spring MVC和Spring Boot的概述, ...

  5. 阿里P7工作总结:Spring MVC的工作原理,看完受益匪浅

    这篇文章将深入探讨Spring框架的一部分--Spring Web MVC的强大功能及其内部工作原理. 项目安装 在本文中,我们将使用最新.最好的Spring Framework 5.我们将重点介绍S ...

  6. Spring MVC 到底是如何工作的

    转载自  Spring MVC 到底是如何工作的 这篇文章将深入探讨Spring框架的一部分--Spring Web MVC的强大功能及其内部工作原理. 这篇文章的源代码可以在GitHub上找到. 项 ...

  7. 中input标签赋值_Java程序员:Spring MVC JSP表单标签示例

    Spring MVC的表单标签为Java程序员提供了许多额外的支持.例如数据绑定,允许自动设置数据并从Java对象中检索数据. 从2.0版本开始,Spring提供了一组全面的数据绑定感知标记,用于在使 ...

  8. Spring MVC 自定义验证器示例

    在任何 spring web mvc 应用程序中,我们经常必须处理表单.应用程序首先显示一个表单,然后用户填写该表单并将其提交给服务器.在服务器上,应用程序需要捕获表单输入并处理输入(例如,存储在数据 ...

  9. [转] 使用Spring MVC构建REST风格WEB应用

    原文地址:http://fancy888.iteye.com/blog/1629120 对于运行在网络上的MIS系统而言,处理数据的是整个系统的主要任务,翻开程序我们可以看到,80%以上的代码都在处理 ...

最新文章

  1. java第一章Java语言概述和入门程序
  2. vyos User Guide
  3. Windows下MySql主从配置实战教程
  4. 电脑打字学习_新手如何学会电脑打字 走上盲打之路
  5. 怎样在IIS下配置PHP
  6. UML统一建模语UML2和EnterpriseArchitect
  7. html中css字体颜色代码大全,css字体颜色的设置方法
  8. 世界三大粮食·水稻稻米十大生产国 国稻种芯百团计划行动
  9. 计算机海报大赛策划书,海报策划书模板.docx
  10. CAJ如何在线免费转换成可编辑的Word
  11. 局域网下访问自己的项目和网页
  12. android 广告库sdk,GitHub - adxdata/sdk-android-demo: 美数广告SDK(Android)示例
  13. 《Web安全渗透全套教程(40集)》学习笔记 | SQL注入攻击及防御
  14. (转)Native Extensions for Silverlight (NESL)?
  15. Particle for alexa smart home skill (1)
  16. (转载)男人应该多吃的菜——凉拌春韭
  17. Z50-70电脑加内存条+加固态硬盘+光驱处加机械硬盘+U盘启动重装系统+第三方装机软件重装系统
  18. Android Studio开发蓝牙应用(二)
  19. 【项目实战案例分享】DMZ区防御体系技战法
  20. 常用正则表达式-IP地址

热门文章

  1. hashmap应用场景_工作中常用到的Java集合有哪些?应用场景是什么?
  2. spring中stereotype注解Component,Repository,Service,Controller
  3. 34.在排序数组中查找元素的第一个和最后一个位置--leetcode算法题解(带注释)
  4. Java版大顶堆的实现
  5. 为wmi执行例外_称之为例外?
  6. jmeter 采样器作用_实施自定义JMeter采样器
  7. 可以自定义模板的ide_将IDE检查应用于自定义Java批注
  8. jdbc事务 jta事务_将非事务性资源绑定到JTA事务中的几种模式
  9. jpa的查询api_为JPA的本机查询API键入安全查询
  10. 具有MicroProfile配置的可配置JAX-RS ExceptionMapper