我正在尝试通过使用swagger解析器从OpenAPI规范生成Json示例,问题是当我尝试运行我的代码时,它返回以下行的异常:
映射
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.oas.inflector.examples.models.Example;
import io.swagger.oas.inflector.examples.ExampleBuilder;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.swagger.oas.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.util.Json;
import java.util.Map;
public class essai {
public static void main(String[] args) throws Exception {
OpenAPI swagger = new OpenAPIV3Parser().read("C:\\Users\\User1\\Desktop\\openapi.yaml");
Map < String, Schema > definitions = swagger.getComponents().getSchemas();
Schema model = definitions.get("Pet");
Example example = ExampleBuilder.fromSchema(model, definitions);
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);
}
}
错误消息是: 线程“主”中的异常java.lang.NullPointerException 在com.example.demo.essai.main(essai.java:16)