2013-05-08 49 views
0

我有具有以下元素JAXB問題與XSD:選擇和xsd:序列

<xs:element name="field"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="type" minOccurs="1" maxOccurs="1"/> 
       <xs:choice> 
        <xs:sequence> 
         <xs:choice> 
          <xs:element ref="content"/> 
          <xs:sequence> 
           <xs:element ref="cmd"/> 
           <xs:element ref="legend"/> 
          </xs:sequence> 
         </xs:choice> 
         <xs:element ref="id" minOccurs="0"/> 
        </xs:sequence> 
        <xs:sequence> 
         <xs:element ref="name"/> 
         <xs:choice> 
          <xs:sequence> 
           <xs:element ref="value"/> 
           <xs:element ref="label"/> 
           <xs:element ref="optional"/> 
          </xs:sequence> 
          <xs:sequence> 
           <xs:element ref="optional"/> 
           <xs:choice> 
            <xs:sequence> 
             <xs:element ref="options"/> 
             <xs:element ref="id" minOccurs="0"/> 
            </xs:sequence> 
            <xs:sequence> 
             <xs:element ref="label"/> 
             <xs:element ref="options"/> 
            </xs:sequence> 
           </xs:choice> 
          </xs:sequence> 
          <xs:sequence> 
           <xs:element ref="label"/> 
           <xs:element ref="optional"/> 
           <xs:choice minOccurs="0"> 
            <xs:element ref="boxes"/> 
            <xs:element ref="id"/> 
            <xs:element ref="options"/> 
           </xs:choice> 
          </xs:sequence> 
         </xs:choice> 
        </xs:sequence> 
        <xs:sequence> 
         <xs:element ref="id"/> 
         <xs:element ref="name"/> 
         <xs:element ref="label"/> 
         <xs:element ref="optional"/> 
        </xs:sequence> 
       </xs:choice> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

架構上生成的類類結構

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
@XmlRootElement(name = "field") 
public class Field { 

    @XmlElementRefs({ 
     @XmlElementRef(name = "legend", type = Legend.class), 
     @XmlElementRef(name = "id", type = JAXBElement.class), 
     @XmlElementRef(name = "name", type = JAXBElement.class), 
     @XmlElementRef(name = "cmd", type = JAXBElement.class), 
     @XmlElementRef(name = "options", type = Options.class), 
     @XmlElementRef(name = "content", type = Content.class), 
     @XmlElementRef(name = "type", type = JAXBElement.class), 
     @XmlElementRef(name = "label", type = Label.class), 
     @XmlElementRef(name = "boxes", type = Boxes.class), 
     @XmlElementRef(name = "optional", type = JAXBElement.class), 
     @XmlElementRef(name = "value", type = JAXBElement.class) 
    }) 
    protected List<Object> content; 

    /** 
    * Gets the rest of the content model. 
    * 
    * <p> 
    * You are getting this "catch-all" property because of the following reason: 
    * The field name "Optional" is used by two different parts of a schema. See: 
    * line 102 of file:/C:/WorkSpaces/src/main/resources/webforms.xsd 
    * line 99 of file:/C:/WorkSpaces/src/main/resources/webforms.xsd 
    * <p> 
    * To get rid of this property, apply a property customization to one 
    * of both of the following declarations to change their names: 
    * Gets the value of the content property. 
    * 
    * <p> 
    * This accessor method returns a reference to the live list, 
    * not a snapshot. Therefore any modification you make to the 
    * returned list will be present inside the JAXB object. 
    * This is why there is not a <CODE>set</CODE> method for the content property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getContent().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link Legend } 
    * {@link JAXBElement }{@code <}{@link String }{@code >} 
    * {@link JAXBElement }{@code <}{@link String }{@code >} 
    * {@link JAXBElement }{@code <}{@link String }{@code >} 
    * {@link JAXBElement }{@code <}{@link String }{@code >} 
    * {@link Content } 
    * {@link Options } 
    * {@link Label } 
    * {@link JAXBElement }{@code <}{@link String }{@code >} 
    * {@link JAXBElement }{@code <}{@link Boolean }{@code >} 
    * {@link Boxes } 
    * 
    * 
    */ 
    public List<Object> getContent() { 
     if (content == null) { 
      content = new ArrayList<Object>(); 
     } 
     return this.content; 
    } 

}  

這是基本的字段分配的所有字段type = JAXBElement.class(ex.-id,但它在實際架構中聲明爲xsd:string) 我試圖從對象 生成json,由於它被分配給JAXBElement,因此生成不正確 generate d「字段
{」declaredType「:」java.lang.String「,」name「:」type「,」value「:」textarea「,」scope「:」javax.xml.bind.JAXBElement $ GlobalScope「 「無」:假的, 「globalScope」:真實的, 「typeSubstituted」:假}

i am looking to get json response like {type:textarea} 

請幫助

回答

0

我解決了這個問題,通過編寫自定義串行現場像下面

公衆class customeSerializer2擴展了JsonSerializer {

@Override 
public void serialize(Field field, JsonGenerator jgen, 
     SerializerProvider provider) throws IOException, 
     JsonProcessingException { 
    jgen.writeStartObject(); 
    JAXBElement jaxbElement; 
    for (Object object : field.getContent()) { 
     if (object.getClass().isAssignableFrom(JAXBElement.class)) { 
      jaxbElement = (JAXBElement) object; 
      // jgen.writeFieldName(jaxbElement.getName().toString()); 
      jgen.writeStringField(jaxbElement.getName().toString(), 
        jaxbElement.getValue().toString());// teRawValue(jaxbElement.getValue().toString()); 
     } else { 
      //jgen.writeStartObject(); 
      jgen.writeObjectField(object.getClass().getSimpleName(),object); 
      //jgen.writeEndObject(); 
     } 
    } 
    jgen.writeEndObject(); 

} 
} 

並使用對象映射器註冊,如

SimpleModule module = new SimpleModule("customeSerializerModule", 
       new Version(1, 0, 0, null)); 
     module.addSerializer(Field.class, new customeSerializer2()); 
     mapper.registerModule(module);