0

JAXB オブジェクトのリストを JSON レスポンスにシリアル化する予定です。現在、以下は私が取得している形式です。以下の応答では、実際に配列を表示している「systemInfoList」の間にもう 1 つのオブジェクトが表示されています。代わりに、dependent_systems_infos に配列 [] を直接表示する必要があります。また、単一のシステム情報応答がある場合でも、配列形式で表示する必要があります。Jackson パーサー 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 に答える