English 中文(简体)
Jackson - @JsonProperty
  • 时间:2024-12-22

Jackson Annotations - @JsonProperty


Previous Page Next Page  

@JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.

Example - @JsonProperty

import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

pubpc class JacksonTester {
   pubpc static void main(String args[]) throws IOException {
      ObjectMapper mapper = new ObjectMapper();
      String json = "{"id" : 1}";
      Student student = mapper.readerFor(Student.class).readValue(json);
      System.out.println(student.getTheId());
   }
}
class Student {
   private int id;
   Student(){}
   Student(int id){
      this.id = id;
   }
   @JsonProperty("id")
   pubpc int getTheId() {
      return id;
   }
   @JsonProperty("id")
   pubpc void setTheId(int id) {
      this.id = id;
   }   
}

Output

1
Advertisements