Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson Cannot Deserialize Object #974

Open
lukaslist opened this issue Apr 10, 2024 · 1 comment
Open

Jackson Cannot Deserialize Object #974

lukaslist opened this issue Apr 10, 2024 · 1 comment

Comments

@lukaslist
Copy link

Error:
[nioEventLoopGroup-3-7] ERROR com.corundumstudio.socketio.JsonSupportWrapper - Can't read value: ["create_expense","{"id":0,"userId":1,"name":"test","amount":123123.0,"type":"EXPENSE","description":"123","date":"2024-04-10"}"] for type: class com.corundumstudio.socketio.protocol.Event
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of dev.lukaslist.models.ExpenseDTO (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":0,"userId":1,"name":"test","amount":123123.0,"type":"EXPENSE","description":"123","date":"2024-04-10"}')
at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION disabled); line: 1, column: 19]

Class:

@Data
@NoArgsConstructor
public class ExpenseDTO {
    private long id;
    private long userId;
    private String name;
    private double amount;
    private String type;
    private String description;
    private LocalDate date;

    @JsonCreator
    public ExpenseDTO(@JsonProperty("id") long id,
                      @JsonProperty("userId") long userId,
                      @JsonProperty("name") String name,
                      @JsonProperty("amount") double amount,
                      @JsonProperty("type") String type,
                      @JsonProperty("description") String description,
                      @JsonProperty("date") LocalDate date) {
        this.id = id;
        this.userId = userId;
        this.name = name;
        this.amount = amount;
        this.type = type;
        this.description = description;
        this.date = date;
    }

    public ExpenseDTO(Expense expense) {
        this.id = expense.getId();
        this.userId = expense.getUser().getId();
        this.name = expense.getName();
        this.amount = expense.getAmount();
        this.type = expense.getType().toString();
        this.description = expense.getDescription();
        this.date = expense.getDate();
    }

    public static List<ExpenseDTO> turnExpenseListToExpenseDTOList(List<Expense> expenses) {
        List<ExpenseDTO> expenseDTOs = new ArrayList<>();
        for(Expense expense : expenses) {
            expenseDTOs.add(new ExpenseDTO(expense));
        }
        return expenseDTOs;
    }
}
@shutuper
Copy link
Contributor

Seems like this error appeared because sender sent stringified object, so the actual value should be replaced:
"{"id":0,"userId":1,"name":"test","amount":123123.0,"type":"EXPENSE","description":"123","date":"2024-04-10"}" ->
{"id":0,"userId":1,"name":"test","amount":123123.0,"type":"EXPENSE","description":"123","date":"2024-04-10"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants