@restcontroller and @responsebody not working in Spring MVC + Thymeleaf
<p>In my spring MVC 4.1.5 application configured to use Thymeleaf 2.1.4 (before it was using JSP and it worked fine), i am unable to return a JSON response. </p>

<p>It always returns a full HTML page weather my request mapping is in a @RestController or if its annotated with @responsebody</p>

<p>Here are the controllers
in an @controller class, i have below mapping</p>

<pre><code>@RequestMapping(value = { "/", "/land", "/login" }, method = RequestMethod.GET)
public String getLogin(Model model, HttpSession session) {
    session.setAttribute("login", "none");
    System.out.println(programId);
    model.addAttribute("signUpForm", new SignUpForm());
    return "login";
}
</code></pre>

<p>and in @RestController class, below is the post method for same URL</p>

<pre><code>    @RequestMapping(value = {"/login" }, method = RequestMethod.POST )
public @ResponseBody HashMap&lt;String, Object&gt; login2(@RequestBody SignUpForm signUpForm,  HttpServletRequest request,
        HttpServletResponse httpServletResponse, HashMap&lt;String, Object&gt; mo, HttpSession session ) {

    User user = userDao.findUserByName(signUpForm.getUserName());

    if (user != null &amp;&amp; encoder.matches(signUpForm.getPassword(), user.getPassword())&amp;&amp; user.getProgram_id()==3) {/* &amp;&amp; user.getProgram_id()==3*/
        session.setMaxInactiveInterval(1200);
        System.out.println(session.getMaxInactiveInterval()+":"+session.getLastAccessedTime()+":"+session.getCreationTime()+":"+session.getServletContext().getContextPath());
        session.setAttribute("login", "success");
        mo.put("redirect", "/home");
        String ipAddress = request.getHeader("X-FORWARDED-FOR");
        if (ipAddress == null) {
            ipAddress = request.getRemoteAddr();
        }
        session.setAttribute("urip", ipAddress);

        return mo;
    } else {
        mo.put("error", "Login failed. Please check your credentials");

        return mo;
    }
}
</code></pre>

<p>Below is my xml configuration</p>

<pre><code>&lt;context:component-scan base-package="com.company.cardholder" /&gt;
&lt;mvc:annotation-driven&gt;
    &lt;mvc:message-converters register-defaults="true"&gt;
        &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
            &lt;property name="objectMapper"&gt;
                &lt;bean class="com.fasterxml.jackson.databind.ObjectMapper"&gt;
                    &lt;property name="serializationInclusion"&gt;
                        &lt;value type="com.fasterxml.jackson.annotation.JsonInclude.Include"&gt;NON_NULL&lt;/value&gt;
                    &lt;/property&gt;
                &lt;/bean&gt;
            &lt;/property&gt;
        &lt;/bean&gt;
    &lt;/mvc:message-converters&gt;
&lt;/mvc:annotation-driven&gt;
&lt;mvc:default-servlet-handler /&gt;
&lt;mvc:interceptors&gt;
    &lt;bean class="com.company.cardholder.session.interceptors.URLInterceptor" /&gt;
&lt;/mvc:interceptors&gt;
&lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
&lt;bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"&gt;
  &lt;property name="prefix" value="/WEB-INF/thymeleaf/" /&gt;
  &lt;property name="suffix" value=".html" /&gt;
  &lt;property name="templateMode" value="HTML5" /&gt;
  &lt;!-- Template cache is set to false (default is true). --&gt;
  &lt;property name="cacheable" value="false" /&gt;
&lt;/bean&gt;
&lt;bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"&gt;
  &lt;property name="templateResolver" ref="templateResolver" /&gt;
&lt;/bean&gt;
&lt;bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"&gt;
 &lt;property name="templateEngine" ref="templateEngine" /&gt;
 &lt;/bean&gt;
</code></pre>

<p>Here is my JSON call</p>

<pre><code>$.ajax({
                type: 'POST',
                url: $form.attr('action'),
                data: JSON.stringify({
                    userName: $form.find('#userName').val(),
                    password: $form.find('#password').val(),
                }),
                contentType: "application/json",
                /*dataType: 'json',*/
                complete: function(data) {
                console.log(data);
                if (data.redirect) {
                    // data.redirect contains the string URL to redirect to
                    window.location.href = data.redirect;
                }else if(data.error){
                    $messageError.text(data.error);
                    $messageError.removeClass('hidden');
                    $messageSuccess.addClass('hidden');
                }
    }
    });
</code></pre>