Spring 3.x JSON status 406 “characteristics not acceptable according to the request ”accept“ headers ()”

后端 未结 14 1463
太阳男子
太阳男子 2020-12-13 01:50

Upon trying to get my response in JSON using Spring 3.x, I get the 406 error \"The resource identified by this request is only capable

14条回答
  •  我在风中等你
    2020-12-13 02:21

    There is nothing wrong in your configuration, let me suggest a few small changes though:

    a) Your namespaces appear wrong - they are referring to the 3.0 schemas, just change them to either 3.1 one's or don't refer to the version explicitly, this way for eg.

    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
    

    OR

    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    

    b) You don't require the ContentNegotiatingViewResolver, you can remove everything but the component-scan and from your configuration

    c) The request will not directly work from the browser as it explicitly requires an Accept header of "application/json" - $.getJson call should work though as it sends the correct headers

    d) Remove the headers=Acc.. from the @RequestMapping, and produces also, both are filtering criteria to match up the correct mapped method call.

    With these, there is no reason why the json should not get served out, can you please try with these and see how it goes.

提交回复
热议问题