Java 在spring boot中将传入JSON映射到类
当我调用spring boot端点时,我很难理解为什么会出现以下错误Java 在spring boot中将传入JSON映射到类,java,json,spring,spring-mvc,spring-boot,Java,Json,Spring,Spring Mvc,Spring Boot,当我调用spring boot端点时,我很难理解为什么会出现以下错误 { "timestamp": 1489573322678, "status": 406, "error": "Not Acceptable", "exception": "org.springframework.web.HttpMediaTypeNotAcceptableException", "message": "Could not find acceptable representation",
{
"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出于某种原因不理解如何将传入的有效负载映射到我定义的类,但我不知道如何解决这个问题。提前感谢您提供的任何帮助。Spring明确指出了此问题: HttpMediaTypeNotAcceptableException 这意味着您在内容类型标题中提供了错误的信息或犯了语法错误。试着放一些类似application/json的东西 也 确保另一端会接受它。您当前仅接受带有值为application/json的accept头的请求。我不认为那是你想要的
因此,要么删除该要求,要么将此标题添加到请求中。请提供您的请求snapshot@MU谢谢我已经用请求更新了问题。是的,我也这么认为,但是我更喜欢在回答之前请求请求快照。谢谢你的回复,但这不是问题所在。Im设置内容类型。我已经附上了我的请求。如果是那样的话,它是另一端。。。更新答案。