HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException: An acceptable view could not be found

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

2015 09 22 04:21:44.297 [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor] Could not parse Accept header: Invalid token character ',' in token "json,application/x-www-form-urlencoded"
2015 09 22 04:21:44.298 [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] Resolving exception from handler [public org.springframework.http.ResponseEntity<java.lang.String> com.areviews.api.restcontroller.APIOrderController.getNewOrderApi(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.lang.Exception]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2015 09 22 04:21:44.298 [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] Invoking @ExceptionHandler method: public org.springframework.web.servlet.ModelAndView com.areviews.web.controller.AbstractController.handleException(java.lang.Exception,javax.servlet.http.HttpServletRequest)
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:115)
    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:129)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:74)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)

      

Controller:

@RequestMapping(value = {"order/add"}, method = RequestMethod.POST, produces="application/json; charset=utf-8")
@ResponseBody
public ResponseEntity<String> getNewOrderApi(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    .....

    return JsonUtils.createJson(jsonObj);
}

      

They use jQuery to query my API:

    $.ajax({
            url: "https://api.mywebsite.com/apiv1/order/add",
            type: 'POST',
            beforeSend: function(xhr) {
                xhr.setRequestHeader("Content-Type","application/json;charset=utf-8");
            },
            data : JSON.stringify(data),
            dataType: 'json',
            success: function(data) {
                if (data.success == true){
                    console.log(data);
                }else{
                    console.log("error: " + data.error_description);
                }
            }
        });

      

The problem is in the "Content Type" request header:

Accept:application/json, text/javascript, */*
Accept-Encoding:gzip, deflate
Accept-Language:zh,zh-TW;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:561
Content-Type:application/x-www-form-urlencoded, application/json;charset=UTF-8

      

We don't know where the "app / x-www-form-urlencoded" ("," creates a problem because it should be ";") comes from. What can I do on my side? What can be done on their side?

+1


source to share


1 answer


I also faced the same problem.

I tried to use Android API using Volley.

There are 2 different types of content. One is the request header Content-Type

and the other is Body Content-Type

. When you initiate a request to POST

or PUT

from volleyball, volleyball request header and check the body Content-Type. If no Content-Type body is specified, the salvo will be added by itself. Volleyball combines both Content-Type and our server was only able to accept one Content-Type. But since volleyball combines Content-Type for header and body, it becomes "application / json, application / x-www-form-urlencoded; charset = utf-8".

Here " application/x-www-form-urlencoded

" is the content type for the request body. So, to solve this problem, you can override the method getBodyContentType()

and return null.



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

      

Do not use return ""

. Usage return ""

also throws an exception.

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

      

Hope this helps.

0


source







All Articles