Java JAXB替换组封送,但不会正确解组

Java JAXB替换组封送,但不会正确解组,java,jaxb,ogc,Java,Jaxb,Ogc,我正在实现OGCWeb功能服务,其中一部分是创建一个从OGC模式继承的功能模式。我的服务可以很好地封送XML,但客户端无法解组XML。我编写了一个测试程序来说明问题: ... ObjectFactory wfsfactory = new ObjectFactory(); net.opengis.gml.v_3_1_1.ObjectFactory gmlfactory = new net.opengis.gml.v_3_1_1.ObjectFactory(); com.example.Object

我正在实现OGCWeb功能服务,其中一部分是创建一个从OGC模式继承的功能模式。我的服务可以很好地封送XML,但客户端无法解组XML。我编写了一个测试程序来说明问题:

...
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");
}
...

任何帮助都将不胜感激。

我完成了这项工作,但有两个警告

您没有为com.example发布
ObjectContext
,因此我使用了手动编码的
ObjectContext
。关键是要包括

substitutionHeadNamespace=”http://www.opengis.net/gml“
substitutionHeadName=“\u功能”

工厂方法的
@xmlementdecl
中的值,否则我会看到相同的症状,即编组正常,解编组为空,但没有异常

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);   
    }
}
package.com.example;
导入javax.xml.bind.JAXBElement;
导入javax.xml.bind.annotation.xmlementdecl;
导入javax.xml.bind.annotation.XmlRegistry;
导入javax.xml.namespace.QName;
@XmlRegistry
公共类对象工厂{
公共对象工厂(){}
public Foo createFoo(){return new Foo();}
@XmlElementDecl(命名空间=”http://example.com",
name=“foo”,
替换名称空间=”http://www.opengis.net/gml",
替换HeadName=“_功能”)
公共JAXBElement createFoo(Foo-Foo){
返回新的JAXBElement(新的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");
        }
    }
}
package.com.example;
导入java.io.StringReader;
导入java.io.StringWriter;
导入java.util.array;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBElement;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.annotation.XmlAccessorType;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlType;
导入javax.xml.bind.annotation.XmlAccessType;
导入net.opengis.gml.v_3_1_1.AbstractFeatureType;
导入net.opengis.gml.v_3_1_1.FeaturePropertyType;
导入net.opengis.wfs.v_1_1_0.FeatureCollectionType;
导入net.opengis.wfs.v_1_1_0.ObjectFactory;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name=“foo”,proporter={“bar”})
公共类Foo扩展了AbstractFeatureType{
@XmlElement
受保护的字符串bar=“0”;
@凌驾
公共对象createNewInstance(){
返回新的Foo();
}
公共静态void main(字符串[]args)抛出JAXBEException{
ObjectFactory wfsfactory=新的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());
setFeatureMember(Arrays.asList(prop));
//封送到XML
JAXBContext ctx=JAXBContext.newInstance(ObjectFactory.class,net.opengis.gml.v_3_1_1.ObjectFactory.class,com.example.ObjectFactory.class);
StringWriter sw=新的StringWriter();
Marshaller=ctx.createMarshaller();
setProperty(marshaller.JAXB_格式的_输出,Boolean.TRUE);
marshaller.Marshall(wfsfactory.createFeatureCollection(featureCollection),sw);
System.out.println(sw.toString());
//解组返回到对象
JAXBElement afterMarshal=(JAXBElement)
ctx.createUnmarshaller().unmarshal(新StringReader(sw.toString());
贾斯贝尔蒙
0
非空

祝你好运!

尝试时会发生什么?在XML被解封后,feature属性为null。它应该包含在封送之前放入的Foo功能。顺便问一下,你知道吗?是的。我们目前正在使用org.jvnet.ogc架构jars。不幸的是,这是WFS规范中需要你的部分执行模式继承以返回由本地要素类型(上面的我的模式)定义的要素列表。我正在使用模式JAR和
xjc
中的绑定文件,似乎可以找到,封送员没有抱怨。只是当上下文读取XML时,出现了问题。非常感谢。我不知道JAXB在ObjectFactory中查找了该信息。
<?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>
package com.example;

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

    protected String bar;

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