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

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

    Shortly:

    For Spring MVC 4.1.6 there is enough:

    
      com.fasterxml.jackson.core
      jackson-databind
      2.5.0
    
    

    jackson-databind has dependency on core and annotations artifacts.


    In details:

    What is HTTP 406 error?

    406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

    It means that Server cannot generate content which MEDIA TYPE stated in Accept Header.

    But how does server know which MEDIA TYPE it can generate and which not?

    Spring Web has concept of HttpMessageConverter. Some of these converters are already registered in Spring and AbstractMessageConverterMethodArgumentResolver holds them in property messageConverters.

    During request processing AbstractMessageConverterMethodProcessor analyzes what spring can convert and saves all supported MEDIA TYPES in producibleMediaTypes variable. If requested MEDIA TYPE is not producible then says Error 406 == I cannot generated requested media type. Sorry.

    To cut the long story short - register required converters. In your case it's jackson library which produces application/json

提交回复
热议问题