1

I've added jackson's jars to my project's lib directory jackson-core-asl and jackson-mapper-asl. I also have wirtten in my dispatcher-servlet.xml the following:

<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
        </list>
    </property>
</bean>

My controller is:

@Controller
public class SaleController {
@RequestMapping(value = "/sales/info", produces = "application/json", headers = "Accept=*/*")
    public @ResponseBody Product getProductJson(@RequestParam String id) throws SQLException
    {
        ProductDAO mapping = new ProductDAOImpl();
        Product product = mapping.getProductById(Integer.parseInt(id));
        return product;
    }
}

But when I'm trying to get this response in the browser I still recieve 406 HTTP status message instead of JSON-response.

NOTE: Before You mark my post as duplicate, I check this, this and this but it doesn't work.

Community
  • 1
  • 1
  • Start by checking if you're sending with the `Content-Type: application/json` header and remove the `headers` attribute from the `@RequestMapping` and see if that works out. – Bart Jul 17 '14 at 07:10
  • You use an request parameter `id` which is not defined in the request mapping. – Jens Jul 17 '14 at 07:12
  • @Bart How to send with Content-Type: application/json? –  Jul 17 '14 at 07:12
  • @DmitryFucintv My bad. I see you only respond with JSON :-) Not sure which Spring MVC version you are using but you generally don't need to declare a message converter for Jackson. Spring will pick it up by itself. Also use Jackson 2+ whenever you can. – Bart Jul 17 '14 at 07:20

1 Answers1

0

With Spring version 3.2.2 there is no need to register any custom message converter

so your configuration should be like this

<context:component-scan base-package="com.your.package.controller" />

<mvc:annotation-driven />

after that have these two dependencies in your POM file

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.2</version>
        </dependency>

you can also try adding a application/json againt Accept header

Ramzan Zafar
  • 1,562
  • 2
  • 17
  • 18