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

后端 未结 14 1444
太阳男子
太阳男子 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:33

    I had similar issue but it was weird. I will explain how I resolved it.

    In my web.xml my dispacher servlet was mapped to *.htm

    
        mvc-dispatcher
        /
    
    

    and no matter what I did it always threw -

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

    Finally I changed it to

    
        mvc-dispatcher
        /
    
    

    and it worked. What I think is when you just specify

     
    

    the extension takes precedence over accept header and .htm was was creating issues. And obviously I could not use xml or json since servlet itself was not mapped.

    I would also like to add that produces annotation that you, spring will try to map it will accept header of the incoming request. I was making request handler method generic for json and xml. Since I am using Java 8 and Jaxb is inbuilt in Java since Java 7 no need for JAXB dependency. For json I only needed to add -

    
    

    I am using ivy for dependency management.

提交回复
热议问题