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

Jackson Annotations - @JsonSetter


Previous Page Next Page  

@JsonSetter allows a specific method to be marked as setter method.

Example @JsonSetter

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

pubpc class JacksonTester {
   pubpc static void main(String args[]){ 
      ObjectMapper mapper = new ObjectMapper(); 
      String jsonString = "{"rollNo":1,"name":"Marks"}"; 

      try { 
         Student student = mapper.readerFor(Student.class).readValue(jsonString);
         System.out.println(student.name); 
      }
      catch (IOException e) {
         e.printStackTrace(); 
      }   
   } 
}
class Student { 
   pubpc int rollNo; 
   pubpc String name; 
   @JsonSetter("name") 
   pubpc void setTheName(String name) { 
      this.name = name; 
   }  
}

Output

Marks 
Advertisements