title: Resolving the view - Spring REST service
body: <p>I want to generate raw xml from a REST service using Spring. Something is getting lost in resolving the views. I am getting the following:</p>
  
  <pre><code>javax.servlet.ServletException: Circular view path [RemedyXml]: would dispatch back to the current handler URL [/audiClave/REST/remedies/RemedyXml] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
  </code></pre>
  
  <p>Here is the web.xml (abbreviated):</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;rest&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/restServlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;rest&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/REST/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>restServlet-context.xml is as follows (abbreviated):</p>
  
  <pre><code>&lt;context:component-scan base-package="com.audiClave.controllers" /&gt;
  &lt;bean id="beanNameResolver" 
      class="org.springframework.web.servlet.view.BeanNameViewResolver"/&gt;
  &lt;bean id = "RemedyXml" class = "com.audiClave.views.RemedyXmlView"/&gt;
  </code></pre>
  
  <p>Now for the controller:</p>
  
  <pre><code>@Controller
  public class RestController{
  private static final String REMEDIES = "remedies";
  
      @RequestMapping(value = "/REST/remedies/{language}", method = RequestMethod.GET)
      protected ModelAndView getAllRemedies(HttpServletResponse response,
          @PathVariable("language") String language) throws IOException {
          Map&lt;String, List&lt;Remedy&gt;&gt; myModel = new HashMap&lt;String, List&lt;Remedy&gt;&gt;();
          RemediesBean remediesBean = Factory.getRemediesBean();
          myModel.put("remedies", remediesBean.loadRemedies(language));
          return new ModelAndView("RemedyXml","model", myModel);
  </code></pre>
  
  <p>And finally the view:</p>
  
  <pre><code>public class RemedyXmlView implements View
  {
  
    /** XML document */
    private Document doc = null;
  
    public RemedyXmlView()
    {
    }
  
    public String getContentType()
    {
      return "text/xml";
    }
  
  @Override
    public void render(Map&lt;String, ?&gt; model, HttpServletRequest request, HttpServletResponse response) 
      throws Exception {
  
      /** Generate the XML */
      // Start document with "root" tag
      this.doc = new Document(new Element("model"));
      // Get the root tag
      Element rootEl = this.doc.getRootElement();
  
      // Add other tags
      Map&lt;String,List&lt;Remedy&gt;&gt; myModel = (Map&lt;String,List&lt;Remedy&gt;&gt;)model.get("model");
      List&lt;Remedy&gt; messages = (List&lt;Remedy&gt;)myModel.get("remedies");
      Iterator&lt;Remedy&gt; iter = messages.iterator();
      while(iter.hasNext()){
        Remedy remedy = iter.next();
  
        // Add new xml element
        Element messageEl = new Element("remedy");
        messageEl.setText(remedy.getName());
        rootEl.addContent(messageEl);
      }
  
      // printXMLDocument(this.doc);
  
      /** Set response type and write XML */
      XMLOutputter outp = new XMLOutputter();
      outp.setFormat(Format.getPrettyFormat());
      String xmlAsString = outp.outputString(doc);
  
      response.setContentType("text/xml");
      response.setContentLength(xmlAsString.length());
  
      PrintWriter out = new PrintWriter(response.getOutputStream());
      out.print(xmlAsString);
      out.flush();
      out.close();    
    }
  </code></pre>
  
  <p>}</p>
  
  <p>Any input would be appreciated, thanks.</p>