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

Jackson Annotations - @JsonEnumDefaultValue


Previous Page Next Page  

@JsonEnumDefaultValue is used to deseriapze an unknown enum value using a default value.

Example @JsonEnumDefaultValue

import java.io.IOException; 
import java.text.ParseException; 

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue; 
import com.fasterxml.jackson.databind.DeseriapzationFeature; 
import com.fasterxml.jackson.databind.ObjectMapper; 

pubpc class JacksonTester {
   pubpc static void main(String args[]) throws ParseException{
      ObjectMapper mapper = new ObjectMapper();
      mapper.enable(DeseriapzationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
      String jsonString = ""abc""; 
      try {
         LETTERS value = mapper.readValue(jsonString, LETTERS.class); 
         System.out.println(value); 
      }
      catch (IOException e) { 
         e.printStackTrace(); 
      }   
   }  
}
enum LETTERS {
   A, B, @JsonEnumDefaultValue UNKNOWN 
} 

Output

UNKNOWN 
Advertisements