title: Spring 2.5.x MVC portlet with annotation & AJAX with jQuery+JSON Error: Content Type cant be set to json
body: <p>When i try to use spring 2.5.x <code>DispatcherPortlet</code> with liferay to use Ajax, i am getting this error. I have configured it using annotations.</p>
  
  <p>At class level i used following annotations,</p>
  
  <p>Code for controller...</p>
  
  <pre><code>@Controller
  @RequestMapping("VIEW")
  public class MyController {
  
  @SuppressWarnings("unchecked")
   @RequestMapping(method=RequestMethod.GET) //this is default rendering...
   public String defaultRender(Model model, PortletRequest request) {
   ...
   return "/some/someview";
   }
  
  
  @RequestMapping(method=RequestMethod.POST,params="action=something") // For ajax call...using jQuery
   public ModelAndView processTotalMatchCount(RenderRequest request, Model model) throws IOException {
    Map&lt;String,String&gt; hmapStockSearchParams = getSomeData(request);  
    Integer totalMatches = service.getTotal(hmapStockSearchParams);
    Map map = new HashMap();
    map.put("totalMatches", totalMatches); 
    return new ModelAndView("ajaxView",map);
   }
  
  }
  </code></pre>
  
  <p>From JSP i am calling following JS function using jQuery.</p>
  
  <pre><code>function fetchTotalMatches(){ 
   var url = "&lt;portlet:renderURL&gt;&lt;portlet:param name='action' value='something' /&gt;&lt;/portlet:renderURL&gt;";
    jQuery.ajax({ url: url,
      method: 'POST', 
      //dataType:'json', when i enable this it goes in error bcs response is not in JSON format...
      data: {param1:'one',param2:'two'},      
      success: function(message) {
       alert(message);
       jQuery('input:hidden[@name="total"]').val(message.totalMatches);     
       jQuery('div.secondary-nav').children('div.viewMatches').
        html(message.totalMatches);
      }, 
      error: function(xhr, ajaxOptions, thrownError) {
       alert(xhr.status + " - " + xhr.statusText);
      }
     }
    );
  }
  </code></pre>
  
  <p>When i enable <code>dataType:'json'</code> in jquery call, it goes in error function. Otherwise i will get the response alert of the whole HTML of the page defined in <code>defaultRender</code> method (The same jsp from which i m calling this function. )</p>
  
  <p>And in console i am getting this error...</p>
  
  <p>please help regarding this. What am i missing?</p>
  
  <p>I have used Ajax related Resolver and View as per this link...</p>
  
  <p><a href="http://www.jtraining.com/blogs/ajax-with-spring-mvc-and-jquery.html" rel="nofollow noreferrer">http://www.jtraining.com/blogs/ajax-with-spring-mvc-and-jquery.html</a></p>
  
  <pre><code>[2010-07-02 11:13:41,703][http-8080-4][ERROR] - org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:501) - Could not complete request
   java.lang.IllegalArgumentException
   at com.liferay.portlet.MimeResponseImpl.setContentType(MimeResponseImpl.java:162)
   at org.springframework.web.portlet.DispatcherPortlet.render(DispatcherPortlet.java:1090)
   at org.springframework.web.portlet.DispatcherPortlet.doRenderService(DispatcherPortlet.java:832)
   at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:483)
   at org.springframework.web.portlet.FrameworkPortlet.doDispatch(FrameworkPortlet.java:453)
   at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
   at com.sun.portal.portletcontainer.appengine.filter.FilterChainImpl.doFilter(FilterChainImpl.java:126)
   at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:69)
   at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:100)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
   at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
   at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
   at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:618)
   at com.liferay.portlet.InvokerPortletImpl.invokeRender(InvokerPortletImpl.java:700)
   at com.liferay.portlet.InvokerPortletImpl.render(InvokerPortletImpl.java:419)
   at org.apache.jsp.html.portal.render_005fportlet_jsp._jspService(render_005fportlet_jsp.java:1467)
   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
   at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
   at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
   at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
   at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
   at com.liferay.portal.util.PortalImpl.renderPortlet(PortalImpl.java:2884)
   at com.liferay.portal.util.PortalUtil.renderPortlet(PortalUtil.java:897)
   at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processPortlet(RuntimePortletUtil.java:170)
   at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processPortlet(RuntimePortletUtil.java:103)
   at com.liferay.portlet.layoutconfiguration.util.velocity.PortletColumnLogic.processContent(PortletColumnLogic.java:133)
   at com.liferay.portlet.layoutconfiguration.util.velocity.TemplateProcessor.processColumn(TemplateProcessor.java:68)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.doInvoke(UberspectImpl.java:389)
   at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:378)
   at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:270)
   at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:252)
   at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:332)
   at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:336)
   at org.apache.velocity.Template.merge(Template.java:328)
   at org.apache.velocity.Template.merge(Template.java:235)
   at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:381)
   at com.liferay.portal.velocity.VelocityEngineImpl.mergeTemplate(VelocityEngineImpl.java:179)
   at com.liferay.portal.kernel.velocity.VelocityEngineUtil.mergeTemplate(VelocityEngineUtil.java:84)
   at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processTemplate(RuntimePortletUtil.java:237)
   at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processTemplate(RuntimePortletUtil.java:190)
   at org.apache.jsp.html.portal.layout.view.portlet_jsp._jspService(portlet_jsp.java:831)
   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
   at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
   at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
   at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
   at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
   at com.liferay.portal.action.LayoutAction.includeLayoutContent(LayoutAction.java:294)
   at com.liferay.portal.action.LayoutAction.processLayout(LayoutAction.java:471)
   at com.liferay.portal.action.LayoutAction.execute(LayoutAction.java:195)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
   at com.liferay.portal.struts.PortalRequestProcessor.process(PortalRequestProcessor.java:157)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
   at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
   at com.liferay.portal.servlet.MainServlet.callParentService(MainServlet.java:608)
   at com.liferay.portal.servlet.MainServlet.service(MainServlet.java:846)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
   at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
   at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
   at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
   at com.liferay.portal.servlet.FriendlyURLServlet.service(FriendlyURLServlet.java:143)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.servlet.filters.strip.StripFilter.processFilter(StripFilter.java:142)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:94)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.servlet.filters.gzip.GZipFilter.processFilter(GZipFilter.java:140)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.servlet.filters.secure.SecureFilter.processFilter(SecureFilter.java:282)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:94)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.servlet.filters.cache.CacheFilter.processFilter(CacheFilter.java:425)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.servlet.filters.autologin.AutoLoginFilter.processFilter(AutoLoginFilter.java:257)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.servlet.filters.sso.opensso.OpenSSOFilter.processFilter(OpenSSOFilter.java:73)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.sharepoint.SharepointFilter.processFilter(SharepointFilter.java:193)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.servlet.filters.virtualhost.VirtualHostFilter.doFilter(VirtualHostFilter.java:191)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.servlet.filters.threadlocalcache.ThreadLocalCacheFilter.processFilter(ThreadLocalCacheFilter.java:55)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:91)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:154)
   at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:94)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   at java.lang.Thread.run(Thread.java:619)
  </code></pre>
  
  <p>Basically its not allowing to set content type to <code>application\json</code> as per the below class</p>
  
  <pre><code>public class AjaxView extends AbstractView {
   private static final Log log = LogFactory.getLog(AjaxView.class);
     private static final String REQUEST_CONTEXT_ATTRIBUTE = RequestContext.class.toString();
  
   public AjaxView() {
  //  super();  
    setRequestContextAttribute(REQUEST_CONTEXT_ATTRIBUTE);
       setContentType("application/json;charset=UTF-8");     
   }
      /* (non-Javadoc)
       * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
       */
      @Override
      protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) 
      throws Exception {
          log.info("Resolving ajax request view - "+map);        
          JSONObject jsonObj = new JSONObject(map);
          log.info("content Type = " + getContentType());     
  //        response.reset();
          response.setContentType("application/json");
          response.setCharacterEncoding("UTF-8");
          response.getWriter().write(jsonObj.toString());
          response.getWriter().flush();
      }
  }
  </code></pre>
  
  <p>Please help ASAP. What i need is, Ajax call returning JSON string in spring 2.5.x MVC Portlet...</p>
  
  <p>thanks in advance.</p>