M
M
Mark Ivanych2016-02-20 03:16:23
Java
Mark Ivanych, 2016-02-20 03:16:23

How to make Jackson throw an exception for variables with no value?

Hello,
there is a pojo object:

public class Comment {
    private int id; // переменная без заданного значения

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Let's say we receive a request from a client, but it does not specify an id , we need to generate an error, but Jackson puts 0 into it . I was hoping for FAIL_ON_NULL_FOR_PRIMITIVES, but it only throws an exception when we explicitly set the {id: null} variable.
I want to cut such fields by default so as not to produce checks for 0.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2016-02-20
@zolt85

Such things are validated somewhere outside. In the case of Spring, you can use validators in controllers.
You can write your own serialization / deserialization for your object, and there you can already fall as you want.
Jackson just serializes/deserializes but doesn't validate them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question