我正在使用Lombok,@注释为我创建了getter、setter和constructors。我还有很多其他的类,杰克逊很容易将它们反序列化。下面是我试图反序列化的对象:
@Value
@Builder
public class RecipeListRemoveDTO {
int recipeListId;
}
在下列控制器方法中使用:
@DeleteMapping(path="/deleteRecipeListFromUser")
public @ResponseBody String deleteRecipeListFromUser(@RequestBody RecipeListRemoveDTO recipeListRemoveDTO) {
return recipeListService.removeRecipeListFromUser(recipeListRemoveDTO);
}
我要发送的JSON:
{
"recipeListId": 2
}
但我收到了错误:
"message": "JSON parse error: Cannot construct instance of com.prepchef.backend.models.dto.RecipeListRemoveDTO (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.prepchef.backend.models.dto.RecipeListRemoveDTO (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (PushbackInputStream); line: 2, column: 5]"
有人知道为什么会这样吗?
发布于 2021-10-08 20:01:40
杰克逊不知道它应该使用Lombok生成的构建器。可能最简单的解决方案是用注解 (从Lombok1.18.14开始)对类进行注释。
@Value
@Builder
@Jacksonized
public class RecipeListRemoveDTO {
int recipeListId;
}
在@Jacksonized
注释下,Lombok执行以下操作(这样您就不需要手动执行这些操作):
@JsonDeserialize(builder=RecipeListRemoveDTO.RecipeListRemoveDTOBuilder.class)
添加到类中,以便Jackson知道它应该使用构建器进行反序列化。@JsonPOJOBuilder(withPrefix="")
添加到构建器类中,以便杰克逊知道buillder方法的名称不是以with
开头。发布于 2021-10-08 07:24:51
https://stackoverflow.com/questions/69490052
复制相似问题