Search code examples
jsonspringspring-mvcthymeleafspring-restcontroller

@restcontroller and @responsebody not working in Spring MVC + Thymeleaf


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.

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

Here are the controllers in an @controller class, i have below mapping

@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";
}

and in @RestController class, below is the post method for same URL

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

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

    if (user != null && encoder.matches(signUpForm.getPassword(), user.getPassword())&& user.getProgram_id()==3) {/* && 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;
    }
}

Below is my xml configuration

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

Here is my JSON call

$.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');
                }
    }
    });

Solution

  • Ok. Here is what i figured out to make it work but i still am not sure of the reason.

    public @ResponseBody HashMap<String, Object> login2(@RequestBody SignUpForm signUpForm,  HttpServletRequest request,
        HttpServletResponse httpServletResponse, HashMap<String, Object> mo, HttpSession session ){
    ////
    }
    

    In the above method signature, i was "injecting" a hashmap. And spring framework default or some unknown config decided to inject a "Binding Result aware Hashmap." I am not sure what difference it would have made. But to fix it, i had to do a

    HashMap<String, Object> mo=new HashMap<String, Object>;
    

    inside the method body and remove the injected hashmap. If anyone is able to understand this behaviour, please explain it. I feel i missed something basic in my awareness of spring framework.