自定义JAXB对象列表到JSON的序列化?
我计划将JAXB对象列表序列化为JSON响应。下面是我目前得到的格式。在下面的响应中,我看到中间还有一个对象是“systemInfoList”,它实际上显示了数组。相反,我希望依赖的系统信息应该直接显示数组[]。此外,如果有单个系统信息响应,也应以数组格式显示。我正在使用Jackson解析器cxf 我目前获得的格式:自定义JAXB对象列表到JSON的序列化?,json,jakarta-ee,jaxb,jsonp,jaxb2,Json,Jakarta Ee,Jaxb,Jsonp,Jaxb2,我计划将JAXB对象列表序列化为JSON响应。下面是我目前得到的格式。在下面的响应中,我看到中间还有一个对象是“systemInfoList”,它实际上显示了数组。相反,我希望依赖的系统信息应该直接显示数组[]。此外,如果有单个系统信息响应,也应以数组格式显示。我正在使用Jackson解析器cxf 我目前获得的格式: { "dependent_systems_infos":{ "systemInfoList":[ { "sy
{
"dependent_systems_infos":{
"systemInfoList":[
{
"system_name":"PZ_Service",
"system_type":"Internal",
"service_infos":[
{
"service_name":"getPZAttributes",
"status":"DOWN",
"response_time_ms":50
}
]
},
{
"system_name":"OMS",
"system_type":"External",
"service_infos":[
{
"service_name":"CreateOrder",
"status":"UP",
"response_time_ms":2000
},
{
"service_name":"CancelOrder",
"status":"UP",
"response_time_ms":500
}
]
}
]
}
}
我需要的格式:
{
dependent_system_infos : [
{
system_name : 'OMS'
system_type: 'External'
services_infos: [
{
service_name : 'CreateOrder'
status : 'UP'
response_time_ms : 2000
},
{
service_name : 'CancelOrder'
status : 'UP'
response_time_ms : 2000
}
]
},
{
system_name : 'PZ_Service'
system_type: 'Internal'
services_infos: [
{
service_name : 'getPZAttributes'
status : 'UP'
response_time_ms : 2000
}
]
}
]
}
我写的JAXB类:
@XmlRootElement(name = "dependent_systems_infos")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItineraryStatusResponse {
private List<SystemInfo> systemInfoList;
@XmlList
public List<SystemInfo> getSystemInfoList() {
return systemInfoList;
}
public void setSystemInfoList(List<SystemInfo> systemInfoList) {
this.systemInfoList = systemInfoList;
}
}
@XmlType(propOrder = {
"systemName",
"systemType",
"serviceInfoList"
})
@XmlAccessorType(XmlAccessType.FIELD)
public class SystemInfo {
@XmlElement(name = "system_name", required = true)
protected SystemName systemName;
@XmlElement(name = "system_type", required = true)
protected SystemType systemType;
@XmlElement(name = "service_infos", required = true)
protected List<ServiceInfo> serviceInfoList;
}
@XmlRootElement(name=“dependent\u systems\u infos”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类行程状态响应{
私有列表系统信息列表;
@XML列表
公共列表getSystemInfoList(){
返回系统信息列表;
}
公共无效设置系统文件夹列表(列表系统信息列表){
this.systemInfoList=systemInfoList;
}
}
@XmlType(比例器={
“系统名”,
“系统类型”,
“服务信息列表”
})
@XmlAccessorType(XmlAccessType.FIELD)
公共类系统信息{
@XmlElement(name=“system\u name”,必需=true)
受保护的系统名系统名;
@XmlElement(name=“system\u type”,必需=true)
受保护的系统类型系统类型;
@xmlement(name=“service_infos”,required=true)
受保护列表服务信息列表;
}
了解如何生成输出会有所帮助,但主要问题是,您正在序列化包含列表的根对象,而实际上您只想序列化列表本身。如果行程状态响应
中有其他字段,您希望输出的列表是什么样的
您可以删除@XmlRootElement
注释,并将列表标记为名为“dependent\u systems\u infos”的元素:
在我使用Jackson 2.2进行测试时,这两种方法都提供了所需的输出
@XmlAccessorType(XmlAccessType.FIELD)
public static class ItineraryStatusResponse {
private List<SystemInfo> systemInfoList;
@XmlElement(name = "dependent_systems_infos", required = true)
public List<SystemInfo> getSystemInfoList() {
return systemInfoList;
}
public void setSystemInfoList(List<SystemInfo> systemInfoList) {
this.systemInfoList = systemInfoList;
}
}
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
mapper.setAnnotationIntrospector(introspector);
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter().withRootName("dependent_systems_infos");
System.out.println(writer.writeValueAsString(systemInfoList));