小编Soo*_*raj的帖子

根据 Java 环境变量中的值创建使用 @JsonIgnore 的自定义注释

我需要创建一个新的注释,用于在环境变量var == false. 我尝试使用JsonAnnotationIntrospector,但无法获得预期的输出。

public class Vehicle {
    String vehicle_name;
    String vehicle_model;
    //getters and setters  
    @MyAnnotation
    public String getVehicle_model() {
        return vehicle_model;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里我需要删除vehicle_model环境变量时的属性var == false

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@JsonIgnore
public @interface MyAnnotation {
}
Run Code Online (Sandbox Code Playgroud)

这是我的自定义注释的声明。有人可以告诉我应该如何编写 Introspector 部分才能获得我需要的功能吗?

提前致谢。

编辑:我的尝试JacksonAnnotationIntrospector

public class MyAnnotationIntrospector extends JacksonAnnotationIntrospector {
@Override
public boolean hasIgnoreMarker(AnnotatedMember annotatedMember) {
    //need this part
   }
 }
Run Code Online (Sandbox Code Playgroud)

的实现ObjectMapper

 ObjectMapper mapper = new ObjectMapper();
 String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
Run Code Online (Sandbox Code Playgroud)

java json jackson annotation-introspector java-annotations

4
推荐指数
1
解决办法
2434
查看次数