📜  Jackson注释-@JsonSetter

📅  最后修改于: 2020-11-16 07:37:33             🧑  作者: Mango


@JsonSetter允许将特定方法标记为setter方法。

例子@JsonSetter

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

public class JacksonTester {
   public 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 { 
   public int rollNo; 
   public String name; 
   @JsonSetter("name") 
   public void setTheName(String name) { 
      this.name = name; 
   }  
}

输出

Marks