2010-12-14 6 views
3

OGC 웹 피처 서비스를 구현 중이며 그 중 일부는 OGC 스키마에서 상속받을 피쳐 스키마를 만드는 중입니다. 내 서비스는 XML을 잘 정리하지만 클라이언트는 XML을 비 정렬화할 수 없다. 나는 문제를 보여 테스터를 썼다 :JAXB 대체 그룹이 마샬링되었지만 올바르게 언 마샬링되지 않습니다.

... 
ObjectFactory wfsfactory = new ObjectFactory(); 
net.opengis.gml.v_3_1_1.ObjectFactory gmlfactory = new net.opengis.gml.v_3_1_1.ObjectFactory(); 
com.example.ObjectFactory exampleFactory = new com.example.ObjectFactory(); 
OgcJaxbManager manager = OgcJaxbManager.getInstance(); 
FeatureCollectionType featureCollection = wfsfactory 
     .createFeatureCollectionType(); 
FeaturePropertyType prop = gmlfactory.createFeaturePropertyType(); 
prop.setFeature(exampleFactory.createFoo(exampleFactory.createFoo())); 
featureCollection.setFeatureMember(Arrays.asList(prop)); 
//marshal to XML 
String xml = manager.marshal(wfsfactory 
     .createFeatureCollection(featureCollection)); 
log.info(xml); 
//unmarshal back to object 
FeatureCollectionType afterMarshal = (FeatureCollectionType) manager 
     .unmarshal(xml); 
JAXBElement<? extends AbstractFeatureType> feature = afterMarshal 
     .getFeatureMember().get(0).getFeature(); 
if (feature == null) { 
    log.info("null"); 
} else { 
    log.info("not null"); 
} 
... 

출력 : 여기에

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ns4:FeatureCollection xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns1="http://www.opengis.net/gml" xmlns:ns4="http://www.opengis.net/wfs" xmlns:ns3="http://www.w3.org/2001/SMIL20/" xmlns:ns9="http://www.opengis.net/wms" xmlns:ns5="http://www.opengis.net/ows/1.1" xmlns:ns6="http://www.opengis.net/ogc" xmlns:ns10="http://example.com" xmlns:ns7="http://www.opengis.net/ows" xmlns:ns11="http://www.w3.org/2001/SMIL20/Language" xmlns:ns8="http://www.opengis.net/wcs/1.1.1"> 
    <ns1:featureMember> 
     <ns10:foo> 
      <ns10:bar>0</ns10:bar> 
     </ns10:foo> 
    </ns1:featureMember> 
</ns4:FeatureCollection> 

null 

내가 OGC 스키마 확장하고있다 :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<xsd:schema version="1.0" targetNamespace="http://example.com" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:example="http://example.com" 
    xmlns:gml="http://www.opengis.net/gml" 
    elementFormDefault="qualified"> 

    <xsd:import namespace="http://www.opengis.net/gml" 
     schemaLocation="http://schemas.opengis.net/gml/3.1.1/base/gml.xsd"/> 

    <xsd:element name="foo" type="example:foo" 
     substitutionGroup="gml:_Feature"/> 

    <xsd:complexType name="foo"> 
     <xsd:complexContent> 
      <xsd:extension base="gml:AbstractFeatureType"> 
       <xsd:sequence> 
        <xsd:element name="bar" type="xsd:string" minOccurs="0"/> 
       </xsd:sequence> 
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 

</xsd:schema> 
:

여기
... 
    <element name="FeatureCollection" type="gml:FeatureCollectionType" substitutionGroup="gml:_Feature"/> 
    <!-- =========================================================== --> 
    <complexType name="FeatureCollectionType"> 
     <annotation> 
      <documentation>Concrete generic feature collection.</documentation> 
     </annotation> 
     <complexContent> 
      <extension base="gml:AbstractFeatureCollectionType"/> 
     </complexContent> 
    </complexType> 
    <!-- =========================================================== --> 
    <complexType name="AbstractFeatureCollectionType" abstract="true"> 
     <annotation> 
      <documentation>A feature collection contains zero or more features.</documentation> 
     </annotation> 
     <complexContent> 
      <extension base="gml:AbstractFeatureType"> 
       <sequence> 
        <element ref="gml:featureMember" minOccurs="0" maxOccurs="unbounded"/> 
        <element ref="gml:featureMembers" minOccurs="0"/> 
       </sequence> 
      </extension> 
     </complexContent> 
    </complexType> 
    <!-- ===== property for feature association ==== --> 
    <element name="featureMember" type="gml:FeaturePropertyType"/> 
    <!-- ============================================================== --> 
    <complexType name="FeaturePropertyType"> 
     <annotation> 
      <documentation>Container for a feature - follow gml:AssociationType pattern.</documentation> 
     </annotation> 
     <sequence minOccurs="0"> 
      <element ref="gml:_Feature"/> 
     </sequence> 
     <attributeGroup ref="gml:AssociationAttributeGroup"/> 
    </complexType> 
    <!-- ============================================================== --> 
    <element name="_Feature" type="gml:AbstractFeatureType" abstract="true" substitutionGroup="gml:_GML"/> 
    <!-- =========================================================== --> 
    <complexType name="AbstractFeatureType" abstract="true"> 
     <annotation> 
      <documentation>An abstract feature provides a set of common properties, including id, metaDataProperty, name and description inherited from AbstractGMLType, plus boundedBy. A concrete feature type must derive from this type and specify additional properties in an application schema. A feature must possess an identifying attribute ('id' - 'fid' has been deprecated).</documentation> 
     </annotation> 
     <complexContent> 
      <extension base="gml:AbstractGMLType"> 
       <sequence> 
        <element ref="gml:boundedBy" minOccurs="0"/> 
        <element ref="gml:location" minOccurs="0"> 
         <annotation> 
          <appinfo>deprecated</appinfo> 
          <documentation>deprecated in GML version 3.1</documentation> 
         </annotation> 
        </element> 
        <!-- additional properties must be specified in an application schema --> 
       </sequence> 
      </extension> 
     </complexContent> 
    </complexType> 
... 

내 스키마입니다

여기 POJO는입니다.생산 :

어떤 도움을 주시면 대단히 감사하겠습니다.

+0

그리고 시도하면 어떻게됩니까? – skaffman

+0

XML의 비 정렬 화 후에 feature 속성은 null입니다. 마샬링되기 전에 넣은 Foo 기능을 포함해야합니다. –

+0

그건 그렇고, 당신은 http://confluence.highsource.org/display/OGCS/Home을 알고 있습니까? – lexicore

답변

4

두 가지주의 사항으로이 작품을 만들었습니다.

com.example에 ObjectContext을 게시하지 않았으므로 핸드 코딩 된 ObjectContext을 사용했습니다. 그렇지 않으면 내가 같은 증상이 OK, 비 정렬 화가 비어 정렬 화, 즉 볼 수 없지만, 예외, 공장 방법에 대한 @XmlElementDecl에서

substitutionHeadNamespace="http://www.opengis.net/gml"substitutionHeadName="_Feature"

값을 포함하는 것이 중요합니다.

com.example.ObjectContext은 다음과 같습니다

package com.example; 

import javax.xml.bind.JAXBElement; 
import javax.xml.bind.annotation.XmlElementDecl; 
import javax.xml.bind.annotation.XmlRegistry; 
import javax.xml.namespace.QName; 

@XmlRegistry 
public class ObjectFactory { 

    public ObjectFactory() { } 

    public Foo createFoo() { return new Foo(); } 

    @XmlElementDecl(namespace="http://example.com", 
      name="foo", 
      substitutionHeadNamespace="http://www.opengis.net/gml", 
      substitutionHeadName="_Feature") 
    public JAXBElement<Foo> createFoo(Foo foo) { 
     return new JAXBElement<Foo>(new QName("http://example.com", "foo"), Foo.class, foo); 
    } 
} 

com.example.Foo가 주를 포함하여, 다음과 같습니다

package com.example; 

import java.io.StringReader; 
import java.io.StringWriter; 
import java.util.Arrays; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBElement; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlAccessType; 

import net.opengis.gml.v_3_1_1.AbstractFeatureType; 
import net.opengis.gml.v_3_1_1.FeaturePropertyType; 
import net.opengis.wfs.v_1_1_0.FeatureCollectionType; 
import net.opengis.wfs.v_1_1_0.ObjectFactory; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "foo", propOrder = { "bar" }) 
public class Foo extends AbstractFeatureType { 

    @XmlElement 
    protected String bar = "0"; 

    @Override 
    public Object createNewInstance() { 
     return new Foo(); 
    } 

    public static void main(String[] args) throws JAXBException { 
     ObjectFactory wfsfactory = new ObjectFactory(); 
     net.opengis.gml.v_3_1_1.ObjectFactory gmlfactory = new net.opengis.gml.v_3_1_1.ObjectFactory(); 
     com.example.ObjectFactory exampleFactory = new com.example.ObjectFactory(); 
     FeatureCollectionType featureCollection = wfsfactory 
       .createFeatureCollectionType(); 
     FeaturePropertyType prop = gmlfactory.createFeaturePropertyType(); 
     prop.setFeature(exampleFactory.createFoo(exampleFactory.createFoo())); 
     featureCollection.setFeatureMember(Arrays.asList(prop)); 
     //marshal to XML 
     JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class, net.opengis.gml.v_3_1_1.ObjectFactory.class, com.example.ObjectFactory.class); 

     StringWriter sw =new StringWriter(); 
     Marshaller marshaller = ctx.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
     marshaller.marshal(wfsfactory.createFeatureCollection(featureCollection), sw); 
     System.out.println(sw.toString()); 

     //unmarshal back to object 
     JAXBElement<FeatureCollectionType> afterMarshal = (JAXBElement<FeatureCollectionType>) 
      ctx.createUnmarshaller().unmarshal(new StringReader(sw.toString())); 

     JAXBElement<? extends AbstractFeatureType> feature = afterMarshal 
       .getValue().getFeatureMember().get(0).getFeature(); 
     if (feature == null) { 
      System.out.println("null"); 
     } else { 
      System.out.println("not null"); 
     } 
    } 
} 

을 그리고 이것은 출력 내가 얻을 :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ns3:FeatureCollection xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns1="http://www.opengis.net/gml" xmlns:ns4="http://example.com" xmlns:ns3="http://www.opengis.net/wfs" xmlns:ns5="http://www.w3.org/2001/SMIL20/" xmlns:ns6="http://www.opengis.net/ogc" xmlns:ns7="http://www.opengis.net/ows" xmlns:ns8="http://www.w3.org/2001/SMIL20/Language"> 
    <ns1:featureMember> 
     <ns4:foo> 
      <ns4:bar>0</ns4:bar> 
     </ns4:foo> 
    </ns1:featureMember> 
</ns3:FeatureCollection> 

not null 

앞으로도 행운을 빌어 요!

+0

고맙습니다. JAXB가 ObjectFactory에서 해당 정보를 찾지 못했다. –