Home > Blockchain >  Jackson failing to deserialize simple JSON
Jackson failing to deserialize simple JSON

Time:10-08

I'm using Lombok, and the @Annotations create getters, setters, and constructors for me. I have many other classes which Jackson deserializes easily. Here is the object I'm trying to deserialize to:

@Value
@Builder
public class RecipeListRemoveDTO {
    int recipeListId;
}

Used within the following Controller method:

@DeleteMapping(path="/deleteRecipeListFromUser")
public @ResponseBody String deleteRecipeListFromUser(@RequestBody RecipeListRemoveDTO recipeListRemoveDTO) {
    return recipeListService.removeRecipeListFromUser(recipeListRemoveDTO);
}

And the JSON I am sending:

{
    "recipeListId": 2
}

But I'm receiving the error:

"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]"

Does anyone know why this may be the case?

CodePudding user response:

Setters are not generated when you use Lomboks @Value annotation that's why you're getting the exception in your code. Instead you should use @Data annotation.

CodePudding user response:

The reason this doesn't work is that when you use @Value in conjunction with @Builder, no public constructor is generated:

Also, any explicit constructor, no matter the arguments list, implies lombok will not generate a constructor. If you do want lombok to generate the all-args constructor, add @AllArgsConstructor to the class.

and

applying @Builder to a class is as if you added @AllArgsConstructor(access = AccessLevel.PACKAGE) to the class

So, in summary, if you wish to maintain the immutability provided by @Value, in this case you also need to add @AllArgsConstructor.

CodePudding user response:

@Value
@Builder
@JsonDeserializer(builder = RecipeListRemoveDTO.RecipeListRemoveDTOBuilder.class)
public final class RecipeListRemoveDTO {
    int recipeListId;
    
    @JsonPOJOBuilder(withPrefix = "")
    public static final class RecipeListRemoveDTOBuilder {}
}
  • Related