![](/img/trans.png)
[英]How To Mapping Multipart file to DTO using Spring Boot for incoming request
[英]Mapping incoming JSON to a class in spring boot
我正在努力理解為什么我在呼叫Spring Boot端點時出現以下錯誤
{
"timestamp": 1489573322678,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Could not find acceptable representation",
"path": "/quotes"
}
這是我發送到服務器的請求
POST /quotes HTTP/1.1
Host: localhost:8080
tamid: 5
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 94370a3f-6165-106f-f27f-44a44093e0d5
{
"test": "works"
}
我希望傳入的JSON請求主體映射到我定義的Java類。 這是課程。
@Embedded
public class QuoteVersion {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
public void validate() {
}
}
我正在使用@Embedded批注與mongodb映射庫一起使用,我希望該庫與我面臨的問題無關
這是控制器方法
@RequestMapping(
path = "/quotes",
method = RequestMethod.POST,
headers = "Accept=application/json",
produces = "application/json"
)
public @ResponseBody QuoteStatus create (@RequestHeader(value = "tamid") String tamId,
@RequestBody QuoteVersion firstQuoteVersion) {
// final QuoteVersion firstQuoteVersion = this.quoteFactory.createQuoteVersion(incomingQuote);
final User currentUser = User.getFromTamId(tamId);
currentUser.can(Permissions.CREATE_QUOTE);
firstQuoteVersion.validate();
final Quote newQuote = new Quote();
newQuote.addVersion(firstQuoteVersion);
this.dataRepository.save(newQuote);
QuoteStatus qs = new QuoteStatus(newQuote);
return qs;
}
我猜測Spring Boot由於某種原因無法理解如何將傳入的有效負載映射到我定義的類,但是我不知道如何解決該問題。 預先感謝您可能需要提供的任何幫助。
春天清楚地表明了這個問題:
HttpMediaTypeNotAcceptableException
這意味着您在content-type
標頭中提供了錯誤的信息或犯了語法錯誤。 嘗試在其中放置諸如application/json
。
也
確保另一端會接受。 您目前僅接受帶有值為application/json
的accept
標頭的請求。 我認為那不是你想要的。
因此,要么刪除該要求,要么將此標頭添加到請求中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.