将Java接口转换为Json架构时出错
我试图将java接口转换为json模式,但它给出了NullPointerException将Java接口转换为Json架构时出错,java,json,jsonschema,Java,Json,Jsonschema,我试图将java接口转换为json模式,但它给出了NullPointerException public interface Contributors { public List<Contributor> contributors(); public interface Contributor { public String name(); public String contributorUrl(); public List<String&g
public interface Contributors {
public List<Contributor> contributors();
public interface Contributor {
public String name();
public String contributorUrl();
public List<String> roles();
}
}
编辑3:
下面是SchemaGeneratorTest的代码
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.reinert.jjschema.exception.TypeException;
import com.github.reinert.jjschema.v1.JsonSchemaFactory;
import com.github.reinert.jjschema.v1.JsonSchemaV4Factory;
public class SchemaGeneratorTest {
private static ObjectMapper mapper = new ObjectMapper();
public static final String JSON_$SCHEMA_DRAFT4_VALUE = "http://json-schema.org/draft-04/schema#";
public static final String JSON_$SCHEMA_ELEMENT = "$schema";
static {
// required for pretty printing
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
public static void main(String[] args) throws JsonProcessingException, TypeException {
JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory();
schemaFactory.setAutoPutDollarSchema(true);
JsonNode productSchema = schemaFactory.createSchema(Contributors.class);
System.out.println(productSchema);
}
}
您正在使用的库仅报告架构中的字段和getter。重命名方法以get开头:
你写过SchemaGeneratorTest吗?如果是这样,请发布代码。您是否尝试调试它?NullPointerException很容易找到。@Nulano已更新。您使用的是库的最新版本吗?从您提供的GitHub来看,这个堆栈跟踪似乎是不可能的。顺便说一句,即使您的接口是一个抽象类,您也不应该得到任何输出,因为库只处理以get或is为前缀的方法。至少根据github src.Hi@nulano的说法,不幸的是,我不能接触接口契约。还有其他方法可以在不更改合同的情况下完成吗?@ArchitMaheshwari不幸的是,如果不修改JsonSchemaGenerator,就无法完成这项工作。您可以尝试在GITHUB上打开一个沿着-ADDE选项的特性请求问题,将所有方法视为吸气剂。这是一个很好的例子,说明了为什么getter应该始终以get或is作为前缀。实际上,如果你真的想,你可以用反射来修改它,但我认为这超出了这个问题的范围。是的,它似乎还不受支持。您是否可以提供任何指针来通过反射进行转换,以便以下库可以用于我的目的。谢谢你的帮助。我一直在想如何找到在isGetter、get.intern或just get中引用的get字符串。然后修改它的内存char[]value字段,使其成为空字符串。@ArchitMaheshwari我添加了一个示例。
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.reinert.jjschema.exception.TypeException;
import com.github.reinert.jjschema.v1.JsonSchemaFactory;
import com.github.reinert.jjschema.v1.JsonSchemaV4Factory;
public class SchemaGeneratorTest {
private static ObjectMapper mapper = new ObjectMapper();
public static final String JSON_$SCHEMA_DRAFT4_VALUE = "http://json-schema.org/draft-04/schema#";
public static final String JSON_$SCHEMA_ELEMENT = "$schema";
static {
// required for pretty printing
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
public static void main(String[] args) throws JsonProcessingException, TypeException {
JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory();
schemaFactory.setAutoPutDollarSchema(true);
JsonNode productSchema = schemaFactory.createSchema(Contributors.class);
System.out.println(productSchema);
}
}
public interface Contributors {
public List<Contributor> getContributors();
}
public interface Contributor {
public String getName();
public String getContributorUrl();
public List<String> getRoles();
}
public class Test {
private static boolean isCorrupted() {
return "haha".startsWith("get");
}
public static void main(String[] args) throws Exception {
String get = "get";
Field value = String.class.getDeclaredField("value");
value.setAccessible(true);
value.set(get, new char[]{});
System.out.println(isCorrupted()); // prints true
}
}