title: Spring MappingJacksonJsonView, how to tell to use it instead of JSP view?
body: <p>I'm trying to use <code>MappingJacksonJsonView</code> with Spring 3.0, without success. I don't know what I'm doing wrong, I think the problem is that I don't know how to tell to use the <code>MappingJacksonJsonView</code> to render a request. I tried to use the same name for view name and bean name of <code>MappingJacksonView</code>, but didn't work. I built a sample test application here: <a href="https://github.com/stivlo/restjson" rel="nofollow">https://github.com/stivlo/restjson</a></p>
  
  <p>In <a href="https://github.com/stivlo/restjson/blob/master/src/main/webapp/WEB-INF/web.xml" rel="nofollow">web.xml</a> I've defined <code>ContextLoaderListener</code> and the mapping for <code>dispatcherServlet</code>.</p>
  
  <p>In <a href="https://github.com/stivlo/restjson/blob/master/src/main/webapp/WEB-INF/servlet-context.xml" rel="nofollow">servlet-context.xml</a> I've added </p>
  
  <pre><code>&lt;mvc:annotation-driven/&gt;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&lt;bean name="jsonView"
      class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/&gt;
  </code></pre>
  
  <p>In <a href="https://github.com/stivlo/restjson/blob/master/src/main/java/org/obliquid/restjson/web/ToDoList.java" rel="nofollow">org.obliquid.restjson.web.ToDoList.java</a> I set the logical view name as <code>jsonView</code>.</p>
  
  <p>However, instead of using <code>MappingJacksonJsonView</code>, it looks for a JSP file, according to my JSP mapping.</p>
  
  <pre><code>message /restjson/WEB-INF/jsp/jsonView.jsp
  description The requested resource (/restjson/WEB-INF/jsp/jsonView.jsp) 
      is not available.
  </code></pre>
  
  <p>What should I change to use <code>MappingJacksonJsonView</code> as a renderer?</p>
  
  <p><strong>UPDATE 1</strong>: In following tests I've found that if I add the following to my servlet-context.xml, JSON rendering works, but my other view, rendered as JSP (home) is not working anymore.</p>
  
  <pre><code>&lt;!-- Resolve views based on string names --&gt;
  &lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /&gt;
  </code></pre>
  
  <p><strong>UPDATE 2</strong>: I removed the <code>BeanNameViewResolver</code> and changed my ToDoList.java to return directly the Collection to be converted in JSON, instead of ModelAndView, with a <code>@ResponseBody</code> annotation, as follows:</p>
  
  <pre><code>@RequestMapping("/toDoList")
  public @ResponseBody List&lt;ToDoItem&gt; test() {
      List&lt;ToDoItem&gt; toDoList = new ArrayList&lt;ToDoItem&gt;();        
      toDoList.add(new ToDoItem(1, "First thing, first"));
      toDoList.add(new ToDoItem(1, "After that, do the second task"));
      return toDoList;
  }
  </code></pre>
  
  <p>In this way it works. Even though the mapping is even more "magical". It makes me wonder, if a similar renderer exists for XML for instance, how does Spring know which renderer to pick?</p>