My project is using JAXB to turn XSD (XML schema) into POJO and cxf to turn the populated classes into JSON. Is there a tool that can take the schema, and generate a sample JSON document for me? Ideally command line, or a 5 line Java snippet.
我的项目是使用JAXB将XSD(XML模式)转换为POJO,使用cxf将填充的类转换为JSON。是否有可以采用模式的工具,并为我生成示例JSON文档?理想情况下,命令行或5行Java代码段。
Functionality-wise, I want something similar to what SoapUI does when you feed it a WSDL (i.e., amongst other things, generate a sample request from schema and pre-populate all strings with a ?
question mark).
功能方面,我想要一些类似于SoapUI在您提供WSDL时所做的事情(即,除其他事项外,从模式生成示例请求并使用?问号预先填充所有字符串)。
I basically want a quick way to check if the XSD schema changes produce the JSON structure I want (so I do care about structure and types, not about values).
我基本上想要一个快速的方法来检查XSD架构更改是否产生我想要的JSON结构(所以我关心结构和类型,而不是关于值)。
NB: I don't want to create a JSON schema, and I can't use a JSON schema instead of an XSD.
注意:我不想创建JSON模式,我不能使用JSON模式而不是XSD模式。
1 个解决方案
#1
1
You can create a json directly from the classes created with jaxb.
您可以直接从使用jaxb创建的类创建json。
Jaxb create pojo classes.
Jaxb创建pojo类。
Any json library can create the json from a pojo instance.
任何json库都可以从pojo实例创建json。
Here are the steps:
以下是步骤:
- Create your xsd
- Create the classes from the xsd using the tool
xjc
- Create an instance of the classes
- Pass the instance to the pojo library and create a
String
from it
创建你的xsd
使用工具xjc从xsd创建类
创建类的实例
将实例传递给pojo库并从中创建一个String
Here an example with faster jackson:
这里有一个更快的杰克逊的例子:
ObjectMapper mapper = new ObjectMapper();
// PojoClass is the class created with xjc from your xsd
PojoClass pojoInstance = new PojoClass();
// Populate pojoInstance as needed
String jsonString = mapper.writeValueAsString(pojoInstance);
System.out.println(jsonString); // Print the pojoInstance as json string
Creating a random object can be done with a code similar to the following. Note that this code creates only primitive types and objects with primitive types or references to other objects. For arrays, list, maps you need to enhance it.
可以使用类似于以下的代码来创建随机对象。请注意,此代码仅创建具有基本类型的原始类型和对象或对其他对象的引用。对于数组,列表,地图,您需要增强它。
public class RandomObjectFiller {
private Random random = new Random();
public <T> T createAndFill(Class<T> clazz) throws Exception {
T instance = clazz.newInstance();
for(Field field: clazz.getDeclaredFields()) {
field.setAccessible(true);
Object value = getRandomValueForField(field);
field.set(instance, value);
}
return instance;
}
private Object getRandomValueForField(Field field) throws Exception {
Class<?> type = field.getType();
if(type.equals(Integer.TYPE) || type.equals(Integer.class)) {
return random.nextInt();
} else if(type.equals(Long.TYPE) || type.equals(Long.class)) {
return random.nextLong();
} else if(type.equals(Double.TYPE) || type.equals(Double.class)) {
return random.nextDouble();
} else if(type.equals(Float.TYPE) || type.equals(Float.class)) {
return random.nextFloat();
} else if(type.equals(String.class)) {
return UUID.randomUUID().toString();
}
return createAndFill(type);
}
}
The previous example using this class is the following code:
上一个使用此类的示例是以下代码:
ObjectMapper mapper = new ObjectMapper();
RandomObjectFiller randomObjectFiller = new RandomObjectFiller();
// PojoClass is the class created with xjc from your xsd
PojoClass pojoInstance = randomObjectFiller.createAndFill(PojoClass.class);
String jsonString = mapper.writeValueAsString(pojoInstance);
System.out.println(jsonString); // Print the pojoInstance as json string
#1
1
You can create a json directly from the classes created with jaxb.
您可以直接从使用jaxb创建的类创建json。
Jaxb create pojo classes.
Jaxb创建pojo类。
Any json library can create the json from a pojo instance.
任何json库都可以从pojo实例创建json。
Here are the steps:
以下是步骤:
- Create your xsd
- Create the classes from the xsd using the tool
xjc
- Create an instance of the classes
- Pass the instance to the pojo library and create a
String
from it
创建你的xsd
使用工具xjc从xsd创建类
创建类的实例
将实例传递给pojo库并从中创建一个String
Here an example with faster jackson:
这里有一个更快的杰克逊的例子:
ObjectMapper mapper = new ObjectMapper();
// PojoClass is the class created with xjc from your xsd
PojoClass pojoInstance = new PojoClass();
// Populate pojoInstance as needed
String jsonString = mapper.writeValueAsString(pojoInstance);
System.out.println(jsonString); // Print the pojoInstance as json string
Creating a random object can be done with a code similar to the following. Note that this code creates only primitive types and objects with primitive types or references to other objects. For arrays, list, maps you need to enhance it.
可以使用类似于以下的代码来创建随机对象。请注意,此代码仅创建具有基本类型的原始类型和对象或对其他对象的引用。对于数组,列表,地图,您需要增强它。
public class RandomObjectFiller {
private Random random = new Random();
public <T> T createAndFill(Class<T> clazz) throws Exception {
T instance = clazz.newInstance();
for(Field field: clazz.getDeclaredFields()) {
field.setAccessible(true);
Object value = getRandomValueForField(field);
field.set(instance, value);
}
return instance;
}
private Object getRandomValueForField(Field field) throws Exception {
Class<?> type = field.getType();
if(type.equals(Integer.TYPE) || type.equals(Integer.class)) {
return random.nextInt();
} else if(type.equals(Long.TYPE) || type.equals(Long.class)) {
return random.nextLong();
} else if(type.equals(Double.TYPE) || type.equals(Double.class)) {
return random.nextDouble();
} else if(type.equals(Float.TYPE) || type.equals(Float.class)) {
return random.nextFloat();
} else if(type.equals(String.class)) {
return UUID.randomUUID().toString();
}
return createAndFill(type);
}
}
The previous example using this class is the following code:
上一个使用此类的示例是以下代码:
ObjectMapper mapper = new ObjectMapper();
RandomObjectFiller randomObjectFiller = new RandomObjectFiller();
// PojoClass is the class created with xjc from your xsd
PojoClass pojoInstance = randomObjectFiller.createAndFill(PojoClass.class);
String jsonString = mapper.writeValueAsString(pojoInstance);
System.out.println(jsonString); // Print the pojoInstance as json string