HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException:找不到可接受的表示 [英] HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException: Could not find acceptable representation

查看:180
本文介绍了HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException:找不到可接受的表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端正在尝试连接的API。但是它会引发错误:

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.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)

控制器:

@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);
}

他们使用jQuery来请求我的API:

They use jQuery to request 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 their Request Header the "Content-Type" is:

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'知道application / x-www-form-urlencoded来自哪里(,会产生问题,因为它应该是;)。我该怎么办?他们可以做些什么?

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

推荐答案

我也面临同样的问题。

I have also faced the same issue.

我试图使用Volley从Android上获取API。

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

有2种不同的Content-Type。一个是Request标头 Content-Type ,另一个是Body Content-Type 。当您从截击,截击请求标头发起 POST PUT 请求时,检查主体内容类型。如果未提及body Content-Type,则会自行添加volley。 Volley结合了Content-Type和我们的服务器只能接受一种Content-Type。但是,由于凌空结合了头部和主体的Content-Type,它变成application / json,application / x-www-form-urlencoded; charset = utf-8。

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".

此处, application / x-www-form-urlencoded 是请求正文的Content-Type。因此,要解决此问题,您可以覆盖 getBodyContentType()方法并返回null。

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;
}

不要使用 return。使用 return也会抛出异常。

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, "

希望这会有所帮助。

这篇关于HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException:找不到可接受的表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆