Java JsonPOJOBuilder-class And Method Code Example
Here is an example of how to use the JsonPOJOBuilder
annotation to deserialize JSON to a Java object using the Jackson library:
import com.fasterxml.jackson.annotation.JsonPOJOBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Example {
public static void main(String[] args) throws IOException {
String json = "{\"name\":\"John\",\"age\":30}";
ObjectMapper objectMapper = new ObjectMapper();
Person person = objectMapper.readValue(json, Person.class);
System.out.println(person.getName());
System.out.println(person.getAge());
}
@JsonPOJOBuilder(withPrefix = "")
public static class PersonBuilder {
}
public static class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
You can also use withPrefix in @JsonPOJOBuilder to specify the prefix for setters like "with" or "set"
@JsonPOJOBuilder(withPrefix = "set")
It will use setter method with "set" prefix for deserialization.