![](/img/trans.png)
[英]HttpMediaTypeNotAcceptableException: Could not find acceptable representation
[英]HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException: Could not find acceptable representation
我有一個客戶端正在嘗試連接的API。 但是它拋出錯誤:
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:
$.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);
}
}
});
問題在於他們的請求標頭中的“內容類型”為:
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
我們不知道“ application / x-www-form-urlencoded”來自何處(“,”會產生問題,因為它應該是“;”)。 我該怎么辦? 在他們這邊可以做什么?
我也面臨同樣的問題。
我試圖使用Volley從Android命中API。
有2種不同的Content-Type。 一個是Request標頭Content-Type
,另一個是Body Content-Type
。 當您從齊射發起POST
或PUT
請求時,齊射采用請求標頭並檢查正文Content-Type。 如果未提及主體Content-Type,則凌空將自行添加。 Volley結合了Content-Type和我們的服務器只能接受一種Content-Type。 但是,由於凌空將Content-Type用作標頭和正文,因此它變成了“ application / json,application / x-www-form-urlencoded; charset = utf-8”。
此處,“ application/x-www-form-urlencoded
”是請求正文的Content-Type。 因此,要解決此問題,可以重寫getBodyContentType()
方法並返回null。
@Override
public String getBodyContentType() {
//return super.getBodyContentType();
return null;
}
不要使用return ""
。 使用return ""
也會引發異常。
[org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor] Could not parse Accept header: Invalid token character ',' in token "json, "
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.