如何将 swagger.json 映射到 Swagger 对象?

我目前正在尝试将 swagger.json 文件映射到 io.swagger.models.Swagger.class。我尝试使用 com.fasterxml.jackson.databind.ObjectMapper.class 通过以下方式解决问题:

new ObjectMapper().readValue(file.getInputStream(), Swagger.class)`

但我得到了这个例外:

"InvalidDefinitionException: Cannot construct instance of io.swagger.models.parameters.Parameter (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: (FileInputStream); line: 28, column: 21] (through reference chain: io.swagger.models.Swagger["paths"]->java.util.LinkedHashMap["/myPath"]->io.swagger.models.Path["post"]->io.swagger.models.Operation["parameters"]->java.util.ArrayList[0])"

问题是 Swagger 类中的参数字段如下所示:

protected Map<String, Parameter> parameters;

这里的Parameter是一个Interface,所以ObjectMapper不知道要实例化哪个类(BodyParameter, PathParameter, FormParameter...)

我希望将 swagger.json 文件加载到 Swagger 对象中。

谢谢您的帮助。


FFIVE
浏览 291回答 2
2回答

慕斯王

与 Rayen 的回答类似,您必须注册反序列化器。Swagger 为其对象(例如参数、属性、模型)提供了序列化io.swagger.util器io.swagger:swagger-core。

慕田峪4524236

我找到了,我需要实现 Deserializers 以明确声明要使用的类构造函数。我对参数、模型和属性都有问题,它们都是接口。这是代码:ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; SimpleModule module = new SimpleModule();&nbsp; &nbsp; &nbsp; &nbsp; module.addDeserializer(io.swagger.models.parameters.Parameter.class, new ParameterDeserializer());&nbsp; &nbsp; &nbsp; &nbsp; module.addDeserializer(io.swagger.models.Model.class, new ModelDeserializer());&nbsp; &nbsp; &nbsp; &nbsp; module.addDeserializer(io.swagger.models.properties.Property.class, new PropertyDeserializer());&nbsp; &nbsp; &nbsp; &nbsp; mapper.registerModule(module);只是一个澄清事情的例子,我以后可以使它更灵活:import java.io.IOException;import com.fasterxml.jackson.core.JsonParser;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.DeserializationContext;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.deser.std.StdDeserializer;import io.swagger.models.parameters.BodyParameter;import io.swagger.models.parameters.FormParameter;import io.swagger.models.parameters.Parameter;import io.swagger.models.parameters.PathParameter;import io.swagger.models.parameters.QueryParameter;public class ParameterDeserializer extends StdDeserializer<io.swagger.models.parameters.Parameter> {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; public ParameterDeserializer() {&nbsp; &nbsp; &nbsp; &nbsp; this(null);&nbsp; &nbsp; }&nbsp; &nbsp; public ParameterDeserializer(Class<?> vc) {&nbsp; &nbsp; &nbsp; &nbsp; super(vc);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Parameter deserialize(JsonParser parser, DeserializationContext ctxt)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws IOException, JsonProcessingException {&nbsp; &nbsp; &nbsp; &nbsp; final JsonNode node = parser.getCodec().readTree(parser);&nbsp; &nbsp; &nbsp; &nbsp; final ObjectMapper mapper = (ObjectMapper) parser.getCodec();&nbsp; &nbsp; &nbsp; &nbsp; if (node.get("in").toString().toLowerCase().replaceAll("\"", "").equals("path")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mapper.treeToValue(node, PathParameter.class);&nbsp; &nbsp; &nbsp; &nbsp; } else if (node.get("in").toString().toLowerCase().replaceAll("\"", "").equals("body")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mapper.treeToValue(node, BodyParameter.class);&nbsp; &nbsp; &nbsp; &nbsp; } else if (node.get("in").toString().toLowerCase().replaceAll("\"", "").equals("query")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mapper.treeToValue(node, QueryParameter.class);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mapper.treeToValue(node, FormParameter.class);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}其他反序列化器也一样。希望能帮助到你。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java