'Jackson java. @JacksonInject dont work while using it's with @JsonDesirializier
I have class Student with builder nested, and @JacksonInject on one field
@JsonDeserialize(builder = Student.Builder.class)
public class Student implements Serializable {
private static final long serialVersionUID = 2L;
@JacksonInject
private int id;
private final String name;
private final int age;
private final double score;
private final boolean olympicGamer;
private Student(String name, int age, double score, boolean olympicGamer) {
this.name = name;
this.age = age;
this.score = score;
this.olympicGamer = olympicGamer;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getScore() {
return score;
}
public boolean isOlympicGamer() {
return olympicGamer;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
olympicGamer == student.olympicGamer &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, olympicGamer);
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", score=" + score +
", olympicGamer=" + olympicGamer +
'}';
}
@JsonPOJOBuilder(withPrefix = "set")
public static class Builder {
private int id;
private String name;
private int age;
private double score;
private boolean olympicGamer;
private Builder() {}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setAge(int age) {
this.age = age;
return this;
}
public Builder setScore(double score) {
this.score = score;
return this;
}
public Builder setOlympicGamer(boolean olympicGamer) {
this.olympicGamer = olympicGamer;
return this;
}
public static Builder create() {
return new Builder();
}
public Student build() {
return new Student(name,age,score,olympicGamer);
}
}
}
But when i try to inject value to field with @JacksonInject it's just dont work
ObjectMapper mapper = new ObjectMapper();
int counter = 1;
InjectableValues inject = new InjectableValues.Std().addValue(int.class,counter);
Student student = mapper.reader(inject).
forType(Student.class).
readValue(request.getInputStream());
try(IStudentDao dao = new StudentDao()) {
dao.create(student);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Student is created but value of field id is always 0. When i'm dont use @JsonDesirializier and @JsonPojoBuilder and use @JsonCreator (with @JsonProperties) it's work. Question is why it's dont work while using @JsonDesirializier?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
