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 {

	@Autowired
	private 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")
	@ResponseBody
	public 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")
	@ResponseBody
	public 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")
	@ResponseBody
	public 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")
	@ResponseBody
	public 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")
	@ResponseBody
	public 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")
	@ResponseBody
	public 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")
	@ResponseBody
	public 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")
	@ResponseBody
	public 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值