![](/img/trans.png)
[英]HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException: Could not find acceptable representation
[英]HttpMediaTypeNotAcceptableException: Could not find acceptable representation
我有以下 controller class:
@RestController
@RequestMapping(value = "/query")
public class QueryController {
@Autowired
private QueryService queryService;
// this post API works perfectly fine
@PostMapping(value = "/submit")
public void submitQuery(@Valid @RequestBody Query query) {
queryService.submit(query);
}
// this API is throwing the error
@GetMapping(value = "/find/email/{email:.+}")
public List<Query> fetchByEmail(@PathVariable("email") String email) {
return queryService.fetchByEmail(email);
}
}
我正在發送以下 cURL 請求:
curl -X GET http://localhost:8562/query/find/email/abc@gmail.com -H 'Content-Type:
在調試模式下,我可以看到請求已到達 controller,並帶有我在請求中發送的預期 emailId。 DAO層搜索也發生了,它返回了1條記錄,但是API拋出以下錯誤:
{
"timestamp": 1569655451392,
"status": 406,
"error": "Not Acceptable",
"message": "Could not find acceptable representation",
"path": "/query/find/email/abc@gmail.com"
}
有人可以幫忙嗎?
注意:我使用的是 springBootVersion = '2.0.5.RELEASE'
編輯: GET API 是引發錯誤的那個。 任何沒有點字符 (.) 的請求都可以正常工作。 以下請求不會引發任何錯誤:
curl -X GET http://localhost:8562/query/find/email/abc@gmail -H 'Content-Type: application/json'
Edit-2:附上下面的調試屏幕截圖:
從這一點開始,如果我運行代碼,我會收到以下錯誤:
{"timestamp":1569657676774,"status":406,"error":"Not Acceptable","message":"找不到可接受的表示形式","path":"/query/find/email/rishi2893@gmail. com"}
HTTP 406 Not-Acceptable 表示服務器無法以客戶端可以理解的格式生成響應。
您必須發送Accept
header 以便通知服務器您的客戶端可以理解的可接受格式,並ensure that your server can produce such formats
。
curl -H "Accept: application/json" ...
除了上述之外,當在請求中發送 Content-Type header 時,它指定了從客戶端發送的請求的媒體類型。 在 POST/PUT 請求中應該使用 Content-Type header。 這在 GET 請求中沒有意義,因為您不應該在此類請求中包含正文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.