【问题标题】:How to add custom annotations to Jackson schema如何向 Jackson 模式添加自定义注释
【发布时间】:2014-04-01 11:30:24
【问题描述】:

我有一堂课,说:

class Data {
    public int value;
};

我想生成 json 模式,使用具有有效数据字段范围的 jackson-schema 模块,例如:

class Data {
    @JsonProperties(min = 1)
    @JsonProperties(max = 100)
    public int value;
};

但是我在 jackson-schema 的 wiki 中搜索,它不支持这一点,是否有一些示例可以让我们自己制作自定义注释?谢谢

【问题讨论】:

    标签: json jackson jsonschema


    【解决方案1】:

    有一种方法可以在杰克逊模式中包含自定义注释,但是您需要创建如下几个类,我使用它来添加我的自定义标题属性:

    1. 创建您自己的SchemaFactoryWrapper

      public static class YourselfSchemaFactoryWrapper extends SchemaFactoryWrapper{
          private static class YourselfSchemaFactoryWrapperFactory extends WrapperFactory {
              @Override
              public SchemaFactoryWrapper getWrapper(SerializerProvider p) {
                  SchemaFactoryWrapper wrapper = new YourselfSchemaFactoryWrapper();
                  if (p != null) {
                      wrapper.setProvider(p);
                  }
                  return wrapper;
              };
      
              @Override
              public SchemaFactoryWrapper getWrapper(SerializerProvider p, VisitorContext rvc) {
                  SchemaFactoryWrapper wrapper = new YourselfSchemaFactoryWrapper();
                  if (p != null) {
                      wrapper.setProvider(p);
                  }
                  wrapper.setVisitorContext(rvc);
                  return wrapper;
              }
          };
      
          public YourselfSchemaFactoryWrapper() {
              super(new YourselfSchemaFactoryWrapperFactory());
              schemaProvider = new YourselfJsonSchemaFactory();
          }
      }
      
    2. 创建您自己的JsonSchemaFactory 您可以在此类中根据需要覆盖 JsonSchemaFactory 的函数。对于这种情况,我只想覆盖字符串模式。

      public class YourselfJsonSchemaFactory extends JsonSchemaFactory{
          @Override
          public StringSchema stringSchema() {
              return new YourselfStringSchema();
          }
      }
      
    3. 创建您自己的 StringSchema

      public class YourselfStringSchema extends StringSchema{
          @Override
          public void enrichWithBeanProperty(BeanProperty beanProperty) {
              super.enrichWithBeanProperty(beanProperty);
              JsonPropertyTitle title = beanProperty.getAnnotation(JsonPropertyTitle.class);
              if(title != null) this.setTitle(title.value());
          }
      }
      
    4. 然后定义自己的注解

      @Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @JacksonAnnotation
      public @interface JsonPropertyTitle
      {
          String value() default "";
      }
      
    5. 最后,你可以在你的模型中使用这个注解

      public class DataModal{
          @JsonPropertyTitle("My Custom Title")
          private str;
      }
      

    【讨论】:

    • @Dhepthi 我正在使用 jackson-module-jsonSchema 2.8.3 并且我工作。你有什么问题吗?
    【解决方案2】:

    您可以为此 (javax.validation.*) 使用 Java 验证 API。

    javax.validation.constraints.Size

    @Size(min = 1, max = 100, message = "error message")
    private String text;
    

    【讨论】:

    • 这应该是评论,不是答案,至少你可以解释更多
    • @Youcef 编辑了我的答案,我的手机没有正确组织。
    【解决方案3】:

    这样做的一个项目是JJSchema。 JSON Schea 的 Jackson 模块目前没有如此细粒度的控制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多