I need to create a new annotation which is used to ignore a field in the output JSON file when the environment variable var == false. I tried to use JsonAnnotationIntrospector, but could not get the expected output.
public class Vehicle {
String vehicle_name;
String vehicle_model;
//getters and setters
@MyAnnotation
public String getVehicle_model() {
return vehicle_model;
}
}
Here I need to remove vehicle_model attribute when the environmental variable var == false.
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@JsonIgnore
public @interface MyAnnotation {
}
This is the declaration of my custom Annotation. Can Someone tell me how should I write the Introspector part to get the functionality I need?
Thanks in advance.
Edit: My attempt at JacksonAnnotationIntrospector
public class MyAnnotationIntrospector extends JacksonAnnotationIntrospector {
@Override
public boolean hasIgnoreMarker(AnnotatedMember annotatedMember) {
//need this part
}
}
And the implementation of ObjectMapper is
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);