我正在尝试将java接口转换为json架构,但它正在给出NullPointerException
public interface Contributors {
public List<Contributor> contributors();
public interface Contributor {
public String name();
public String contributorUrl();
public List<String> roles();
}
}
编辑2: 我得到以下输出:
{"type":"object","$schema":"http://json-schema.org/draft-04/schema#"}
编辑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);
}
}
答案 0 :(得分:1)
您使用的库仅报告架构中的字段和getter。将您的方法重命名为get
:
public interface Contributors {
public List<Contributor> getContributors();
}
public interface Contributor {
public String getName();
public String getContributorUrl();
public List<String> getRoles();
}
编辑:如果你不能修改接口,你可以使用这个代码来破坏&#34; get&#34;字符串并让它打印所有方法无论如何。请不要在实际的生产代码中使用它,因为你会给自己带来很多麻烦。
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
}
}