<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=tanmoy.banerjee" rel="nofollow">tanmoy banerjee</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-7432?redirect=false" rel="nofollow">SPR-7432</a></strong> and commented</p>
<p dir="auto">Hi,</p>
<p dir="auto">In Spring pet-clinic, the implementation is an MVC application serving a browser client. Hence, ModelANdView with<br>
contentNegotiationViewResolver works fine.</p>
<p dir="auto">From the pet-clinic application:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@RequestMapping(value=&quot;/owners/*/pets/{petId}/visits&quot;, method=RequestMethod.GET)
	public ModelAndView visitsHandler(@PathVariable int petId) {
		ModelAndView mav = new ModelAndView(&quot;visits&quot;);
		mav.addObject(&quot;visits&quot;, this.clinic.loadPet(petId).getVisits());
		return mav;
	}
"><pre class="notranslate"><code class="notranslate">@RequestMapping(value="/owners/*/pets/{petId}/visits", method=RequestMethod.GET)
	public ModelAndView visitsHandler(@PathVariable int petId) {
		ModelAndView mav = new ModelAndView("visits");
		mav.addObject("visits", this.clinic.loadPet(petId).getVisits());
		return mav;
	}

</code></pre></div>
<p dir="auto">I was trying to create a simple application which has a Restful Service provider, with browser and non-browser clients. My sample application ( mvc application - for browser client) - given in the spring forum</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@RequestMapping(value = &quot;products/search&quot;, method = RequestMethod.GET)
	public ModelAndView readAllProducts() {
		List&lt;Product&gt; products = productService.findAllProducts();
		ModelAndView mav = new ModelAndView();
		mav.addObject(&quot;products&quot;, products);
		mav.setViewName(&quot;products&quot;);
		return mav;
	}

@RequestMapping(value = &quot;products/new&quot;, method = RequestMethod.POST)
	public String createProduct(@ModelAttribute Product product) {
		
	Product createdProduct = productService.saveOrUpdate(product);
	return &quot;redirect:/products/search&quot;;
	}"><pre class="notranslate"><code class="notranslate">@RequestMapping(value = "products/search", method = RequestMethod.GET)
	public ModelAndView readAllProducts() {
		List&lt;Product&gt; products = productService.findAllProducts();
		ModelAndView mav = new ModelAndView();
		mav.addObject("products", products);
		mav.setViewName("products");
		return mav;
	}

@RequestMapping(value = "products/new", method = RequestMethod.POST)
	public String createProduct(@ModelAttribute Product product) {
		
	Product createdProduct = productService.saveOrUpdate(product);
	return "redirect:/products/search";
	}
</code></pre></div>
<p dir="auto">Also, when the controller is created for a rest client,by creating similar methods a bit differently, mainly changing the <code class="notranslate">@ModelAttribute</code> to <code class="notranslate">@RequestBody</code> and the return type of the method to <code class="notranslate">@ResponseBody</code> Product from ModelAndView, it works fine.</p>
<p dir="auto">(mvc restful application - for non-browser client)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@RequestMapping(value = &quot;products/search&quot;, method = RequestMethod.GET)
	public @ResponseBody
	List&lt;Product&gt; getProducts() {
		return productService.findAllProducts();
	}

@RequestMapping(value = &quot;products/new&quot;, method = RequestMethod.POST)
	public @ResponseBody
	Product createProduct(@RequestBody Product product) {

		return productService.saveOrUpdate(product);
	}"><pre class="notranslate"><code class="notranslate">@RequestMapping(value = "products/search", method = RequestMethod.GET)
	public @ResponseBody
	List&lt;Product&gt; getProducts() {
		return productService.findAllProducts();
	}

@RequestMapping(value = "products/new", method = RequestMethod.POST)
	public @ResponseBody
	Product createProduct(@RequestBody Product product) {

		return productService.saveOrUpdate(product);
	}
</code></pre></div>
<p dir="auto">However, when the requirement is like this that the application should have a normal MVC behavior, as well as serve non-browser REST clients, currently there isn't a way out other than duplicating the methods/creating separate URIs though the resource to be delivered is the same.</p>
<p dir="auto">I feel that this should be possible that when i type in the URI.html in a browser , i get the the JSP rendered HTML; while if I run a REST client (non-browser), it should be able to provide me the json/xml/atom as per the media type requested. Currently this is not easy/intuitive as the MVC is returning ModelAndView, or String view but this is not working out for a REST client for which forcefully using a ModelAndVIew doesn't make sense.</p>
<p dir="auto">Similarly, for MVC, <code class="notranslate">@ModelAttribute</code> is to be used. For REST, where restTemplate POSTs to the URI, <code class="notranslate">@ModelAttribute</code> doesnt work and has to be replaced by <code class="notranslate">@RequestBody</code>.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Test
	public void testPostProduct() {

		URI createProductURI = getURI(PRODUCTS_NEW);

		Product product = mockNewProduct();

		HttpEntity&lt;Product&gt; entity = new HttpEntity&lt;Product&gt;(product);

		Product productReturned = this.restTemplate.postForObject(
				createProductURI, product, Product.class);

		Assert.assertEquals(product, productReturned);

	}"><pre class="notranslate"><code class="notranslate">@Test
	public void testPostProduct() {

		URI createProductURI = getURI(PRODUCTS_NEW);

		Product product = mockNewProduct();

		HttpEntity&lt;Product&gt; entity = new HttpEntity&lt;Product&gt;(product);

		Product productReturned = this.restTemplate.postForObject(
				createProductURI, product, Product.class);

		Assert.assertEquals(product, productReturned);

	}
</code></pre></div>
<p dir="auto">Let me know if</p>
<ol dir="auto">
<li>
<p dir="auto">ModelAndView/ResponseBody - ResponseEntity can be made compatible to be used in either ways</p>
</li>
<li>
<p dir="auto">Same for <code class="notranslate">@ModelAttribute</code> and <code class="notranslate">@RequestBody</code></p>
</li>
<li>
<p dir="auto">Are there anty architecture concerns/reasons for not providing such an option. Though I understand the two ways are for different purposes, I think the API should not restrict this.</p>
</li>
</ol>
<p dir="auto">Thanks,</p>
<p dir="auto">Tanmoy</p>
<hr>
<p dir="auto"><strong>Reference URL:</strong> <a href="http://forum.springsource.org/showthread.php?t=93358" rel="nofollow">http://forum.springsource.org/showthread.php?t=93358</a></p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=martino" rel="nofollow">Martin Ouellet</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-3490?redirect=false" rel="nofollow">SPR-3490</a></strong> and commented</p>
<p dir="auto">In method "public void afterPropertiesSet()" of EhCacheFactoryBean, only cache region created on the fly (i.e. they have no named definition in the xml ehcache configuration file) seems to be decorated.   When one defines a named cache, it will never be decorated eventhough one provides a non null CacheEntryFactory property.</p>
<p dir="auto">We should have these included in the appropriate condition:</p>
<p dir="auto">if (this.cacheManager.cacheExists(this.cacheName)) {<br>
...<br>
Ehcache decoratedCache = decorateCache(this.cache);<br>
this.cacheManager.replaceCacheWithDecoratedCache(this.cache, decoratedCache);<br>
this.cache = decoratedCache;<br>
}<br>
else ...</p>
<p dir="auto">Martin</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.0.5</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398081153" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/8594" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/8594/hovercard" href="https://github.com/spring-projects/spring-framework/issues/8594">#8594</a> EhCacheFactoryBean.setBlocking doesn't work for named caches. (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>