0

我计划将 JAXB 对象列表序列化为 JSON 响应。目前以下是我得到的格式。在下面的响应中,我看到中间的另一个对象是“systemInfoList”,它实际上正在显示数组。相反,我希望dependent_systems_infos 应该直接显示数组[]。此外,如果有单个系统信息响应,它也应该以数组格式显示。我正在使用杰克逊解析器,cxf。

目前我得到的格式:

{
    "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;

}
4

1 回答 1

0

了解如何生成输出会有所帮助,但主要问题是,当您真的只想序列化列表本身时,您正在序列化包含列表的根对象。如果ItineraryStatusResponse其中有其他字段,您希望输出的列表看起来像什么?

您可以删除@XmlRootElement注释并将列表标记为名为“dependent_systems_infos”的元素:

@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;
  }
}

如果您自己进行序列化,另一种方法是ItineraryStatusResponse完全删除对象(因为它只是列表的包装器),然后使用SerializationFeature.WRAP_ROOT_VALUE = true您提供的根名称序列化列表本身:

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));

在我使用 Jackson 2.2 进行的测试中,这两种方法都提供了所需的输出。

于 2013-08-16T17:31:42.723 回答