HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException: Could not find acceptable representation

后端 未结 1 1624
忘掉有多难
忘掉有多难 2021-01-23 16:32

I have a API that a client is trying to connect to. However it throws the error:

2015 09 22 04:21:44.297 [org.springframework.web.servlet.mvc.method.annotation.H         


        
相关标签:
1条回答
  • 2021-01-23 17:04

    I have also faced the same issue.

    I have tried to hit the API from android using Volley.

    There are 2 different Content-Type. One is Request header Content-Type and other is Body Content-Type. When you initiate a POST or PUT request from the volley, volley take request header and check for body Content-Type. If body Content-Type is not mentioned, volley will add by itself. Volley combines both the Content-Type and our server was able to accept only one Content-Type. But, as volley combines Content-Type for header and body it become "application/json, application/x-www-form-urlencoded; charset=utf-8".

    Here, "application/x-www-form-urlencoded" is Content-Type for request body. So, to solve this problem, you can override getBodyContentType() method and return null.

    @Override
    public String getBodyContentType() {
    //return super.getBodyContentType();
        return null;
    }
    

    Do not use return "". Using return "" will also throw exception.

    [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor] Could not parse Accept header: Invalid token character ',' in token "json, "
    

    Hope this will help.

    0 讨论(0)
提交回复
热议问题