繁体   English   中英

在Spring Boot中将传入的JSON映射到类

[英]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/jsonaccept标头的请求。 我认为那不是你想要的。

因此,要么删除该要求,要么将此标头添加到请求中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM