Q: Error 406 in Spring MVC - migration from Spring 3.0.5 to Spring 4.2.4 I am in the process of migrating a web-app from Spring 3.0.5 to 4.2.4. After many hurdles (apparently the process is not really documented well...), I've hit the dreaded 406 error. It seems there are many questions all around, but none of the answers are working (even the questionable ones).
This is the controller code with the function:
@Controller
public class TestController {

@RequestMapping(value = "/echo", method = RequestMethod.GET, headers = "Accept=*/*")
@ResponseBody
public String echoReply(HttpServletRequest request) throws Exception {
    return "echo";
}

@RequestMapping(value = "/test", method = RequestMethod.GET, headers = "Accept=*/*")
@ResponseBody
public boolean testFunc(HttpServletRequest request) throws Exception {
    ...
    return ...;
}

The first function returns echo as expected, but the test function returns error 406 - Could not find acceptable representation. The exact message is:
The resource identified by this request is only capable of generating
responses with characteristics not acceptable according to the request
"accept" headers

Both are relevant to AJAX calls and to direct access via URL.
Any ideas what to look for and why it happens that way?
Edit: perhaps this is some issue in controller class annotations (something missing that should be in Spring 4.2)?
app-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"

   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd" default-lazy-init="true">

    <context:annotation-config />
    <aop:aspectj-autoproxy />
    <tx:annotation-driven/>
    <mvc:annotation-driven/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
       <property name="persistenceUnitName" value="..." />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

    // bunch of component-scans and some more beans

</beans>

pom.xml:
(relevant dependencies for Jackson)
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.1-1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-base</artifactId>
        <version>2.7.1</version>
    </dependency>


A: The official Spring MVC documentation lists the following data types as valid return types for controller methods:


*

*ModelAndView

*Model

*Map

*View

*String

*void

*HttpEntity

*HttpHeaders

*Callable

*DeferredResult

*ListenableFuture

*ResponseBodyEmitter

*SseEmitter

*StreamingResponseBody
For a controller method annotated with @ResponseBody the return type is written to the response body.  This process is carried out using an HttpMessageConverter appropriate for the Accepts request header.

Since your testFunc method has a return type of boolean, Spring MVC will attempt to write the returned value using an HttpMessageConverter.  This means, the Accepts header must specify a value that will result in a valid representation of a boolean value.  Therefore, the representation can only be schemaless, such as JSON.  Any other representation will lead to the described error.
Therefore, if your Accepts header is set to text/html or application/xml, boolean will fail to get written to the response body.  If however you set the header to application/json, your boolean value will get written correctly.
If you want to support HTML and XML as well, you will have to wrap the boolean in an object such as a Map or a custom object.

A: Try to update app-context.xml with
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

