jackson Sping Boot JSON解码错误:无法识别的字段,即使DTO具有这些字段

yyhrrdl8  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(60)

我是Sping Boot 和Java的新手,所以请原谅我的无知。我试着用谷歌搜索,但他们都指向我Jackson错误,这就是这是什么,但我想我是在使用Jackson隐式地使用@RequestBody注解在我的控制器(纠正我,如果我错了)。我有一个看起来像这样的Sping Boot 控制器

@PostMapping(path = "/issues")
public Issue saveIssue(@RequestBody Issue issue) {
    LOG.info("Saving issue");
    return repository.save(issue);
}

dto看起来像

@Data
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "")
public class Issue {

    @DynamoDBHashKey
    private String id;

    @DynamoDBAttribute
    private String type;
}

我将lombok用于@NoArgsConstructor和@AllArgsConstructor注解。
issueRepository只是遵循简单的存储库模式,并使用DynamoDBMapper保存问题。我想这并不重要,因为代码不会到达这一点。由于无效的参数或其他原因,它无法进入控制体。
无论如何,现在我正在做一个帖子请求,看起来像这样:

curl --location 'localhost:8080/issues' \
--header 'Content-Type: application/json' \
--header 'Cookie: JSESSIONID=DC92631F992FC3EA38F7AF9C0080F856; hello=world; howdy=planet' \
--data '{
    "id: "123"
    "type": "code"
}'

我得到错误

[ServerWebInputException: 400 BAD_REQUEST "Failed to read HTTP message"; nested exception is org.springframework.core.codec.DecodingException: JSON decoding error: Unrecognized field "id" (class <package>.issues.dto.Issue), not marked as ignorable; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "id" (class io.atlassian.micros.snykeventforwarder.issues.dto.Issue), not marked as ignorable (0 known properties: ])

当我的dto类明确定义了id和type时,为什么只有0个已知属性?

4zcjmb1e

4zcjmb1e1#

您的curl请求缺少“after id属性。但我认为问题应该是关于能见度。尝试将public String id;只是为了测试如果成功了,问题就在于可见性。

相关问题