2

When I invoke this URL in the browser:

http://localhost:8080/app/foo.json

Spring responds with 406 Status code in this error message:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

However I have defined my content negotiating view resolver like this:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="favorPathExtension" value="true" />
    <property name="ignoreAcceptHeader" value="true" />

    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
            <entry key="html" value="text/html"/>
        </map>
    </property>
</bean>

And my controller is defined like this:

@Controller
@RequestMapping(value = "/foo")
public class ToowootController {

    @RequestMapping(method = GET)
    @ResponseBody
    public FooDTO index() {
        // return fooDTO
    }

}

Any ideas what am I doing wrong?

K Everest
  • 1,565
  • 5
  • 15
  • 23

5 Answers5

10

Looks like the problem was that I had not added all the necessary dependencies, in this case jackson-mapper-asl. I thought only adding jackson-core-asl would be enough.

K Everest
  • 1,565
  • 5
  • 15
  • 23
2

@ResponseBody and ContentNegotiatingViewResolver are two alternatives for the same thing. You usually use one or the other, not both. Remove @ResponseBody if you want to use ContentNegotiatingViewResolver.

Rossen Stoyanchev
  • 4,910
  • 23
  • 26
0

The ContentNegotiationManager solve this problem. Im using spring 3.2. I have already answered, its working for me, especially for .json, check here .

Community
  • 1
  • 1
Sridhar
  • 1,832
  • 3
  • 23
  • 44
0

When you use your website to ContentNegotiatingViewResolver ModelAndViews return controllers or view names and the ContentNegotiatingViewResolver will, based on Various criteria, choose the right data representation strategy.

Therefore you can not only return an object DTO.

Example Configuration

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="order" value="1" />
   <property name="mediaTypes">
    <map>
      <entry key="json" value="application/json" />
      <entry key="xml" value="application/xml" />
    </map>
  </property>
  <property name="defaultViews">
    <list>
       <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"            />
    </list>
  </property>
 <property name="ignoreAcceptHeader" value="true" />
</bean>


@Controller
@RequestMapping(value = "/foo")
public class ToowootController {

    @RequestMapping(method = GET)
    @ResponseBody
    public ModelAndView index() {

        return new ModelAndView("xxxx", "Foo", FooDTO);
    }
 }

The extension will tell Spring the content type to use when returning the data.

Jhonathan
  • 1,611
  • 2
  • 13
  • 24
-3

I'm not sure if this will fix it, but I do see a problem with your code. The @ResponseBody annotation should come before the returned item, in this case, FooDTO. Also I hope you statically imported 'RequestMethod.GET'. Otherwise you should change 'GET' to 'RequestMethod.GET'. So the method should read:

@RequestMapping(method = RequestMethod.GET)
    public @ResponseBody FooDTO index() {
        // return fooDTO
    }
Arun V
  • 590
  • 6
  • 20
  • 3
    No unfortunately this didn't change anything. Putting `@ResponseBody` before the return type is just a "style", otherwise Java does not support annotations on method return types at the moment. And yes, I've statically imported `RequestMethod.GET`. – K Everest Feb 11 '12 at 03:38