我有一个名为 primarySkillStr
的 JSON 字符串:
[
{
"id": 3,
"roleIds": [
2
],
"rating": 2
}
]
我尝试将它映射到一个对象,如下所示:
primarySkillList = mapper.readValue(primarySkillStr,
new TypeReference<List<PrimarySkillDTO>>() {});
但是当我将其转换为 List
时,roleIds
List 为 null
。
是我做错了什么,还是有其他办法?
这是我的DTO
public class PrimarySkillDTO {
private Integer id;
private Integer rating;
private List<Integer> roleIds;
private String name;
}
我在 PrimarySkillDTO
类中有以下注释
@Data
@Builder
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
最佳答案
问题是您的 JsonNaming
注释需要 snake_case 而您没有使用它。
解决
- 删除注解
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
- 或者,将 JSON 字符串中的变量重命名为
role_ids
关于java - 如何使用 ObjectMapper 将 String 转换为 java8 中的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42389386/