Swagger定义未按预期显示

时间:2017-05-15 17:35:34

标签: java dropwizard swagger-ui

我有以下一段代码,并且招摇没有出现我对swagger ui的期望。我使用注释来构建swagger定义。我尝试使用@API和@JsonIgnore。两者都不适合我。

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import io.swagger.annotations.Api;


@JacksonXmlRootElement(localName = "traction")
@Api(hidden = true)
public class Traction
{
  private JsonNode traction;

  public JsonNode getTraction()
  {
    return traction;
  }

  public void setTraction(final JsonNode traction)
  {
    this.traction = traction;
  }

}

Swagger定义显示如下 -

"tractionParent": {
    "traction": {
      "array": false,
      "null": false,
      "float": false,
      "containerNode": false,
      "missingNode": false,
      "nodeType": "ARRAY",
      "valueNode": false,
      "object": false,
      "pojo": false,
      "number": false,
      "integralNumber": false,
      "short": false,
      "int": false,
      "long": false,
      "double": false,
      "bigDecimal": false,
      "bigInteger": false,
      "textual": false,
      "boolean": false,
      "binary": false,
      "floatingPointNumber": false
    }
  }

我需要它显示为

"tractionParent": {
    "traction": {
     }
 }

2 个答案:

答案 0 :(得分:0)

你已经采取了牵引力'变量类型JsonNode,了解为什么要获取Class com.fasterxml.jackson.databind.JsonNode 的所有属性。 有关详细信息,请访问链接 JsonNode Class 以查看源代码

答案 1 :(得分:0)

我最近遇到了同样的问题,并通过注册了一个自定义ModelConverter来解决此问题,该自定义JsonNode映射到free form object只是招摇摇曳

class JsonNodeProperty extends AbstractProperty {
    protected boolean additionalProperties = true;

    public JsonNodeProperty() {
        setType("object");
    }

    public boolean isAdditionalProperties() {
        return additionalProperties;
    }

    public void setAdditionalProperties(boolean additionalProperties) {
        this.additionalProperties = additionalProperties;
    }
}

class JsonNodeModelConverter implements ModelConverter {
    @Override
    public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> chain) {
        JavaType javaType = Json.mapper().constructType(type);
        if (javaType != null) {
            Class<?> clazz = javaType.getRawClass();
            if (JsonNode.class.isAssignableFrom(clazz)) {
                return new JsonNodeProperty();
            }
        }
        if (chain.hasNext()) {
            return chain.next().resolveProperty(type, context, annotations, chain);
        } else {
            return null;
        }
    }

    @Override
    public Model resolve(Type type, ModelConverterContext context, Iterator<ModelConverter> chain) {
        if (chain.hasNext()) {
            return chain.next().resolve(type, context, chain);
        } else {
            return null;
        }
    }
}

您可以通过以下方式注册自定义转换器

ModelConverters.getInstance().addConverter(new JsonNodeModelConverter());