1

Can I get MOXy to convert a string to a boolean when generated jsonとほぼ同じことをしようとしています。

私はxmlから行きます->オブジェクト。オブジェクトには、GeoJson として出力したいジオメトリが含まれるようになりました。

xml-bindings.xml に xml-java-type-adapters を追加しようとしましたが、実行されないようです。package-info.java の注釈は実行されますが、object->json だけでなく、xml-> object にも影響します。そのため、xml マッピングのオーバーライドは良いアイデアだと思いました。

今、私は持っています

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings package-name="se.lantmateriet.geoaccess.outrettfastighetsomrade.jaxb" xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd" version="2.4">
<java-types>
    <java-type name="OutrettFastighetsomradeReferensType">
        <java-attributes>
            <xml-element java-attribute="centralpunkt">
                <xml-java-type-adapter value="se.lantmateriet.sercxi.web.controller.GeoJsonAdapter"/>
            </xml-element>
        </java-attributes>
    </java-type>
</java-types>    

そしていくつかのデモコード:

    List<OutrettFastighetsomradeReferensType> result = this.outrettFastighetsomradeReferensRepository
        .getByExternIdStartsWith(decodeUtf8String(externId));

    List<InputStream> metadata = Lists.newArrayList(new ClassPathResource("json-bindings.xml").getInputStream());

    Map<String, List<InputStream>> properties = ImmutableMap.of(JAXBContextProperties.OXM_METADATA_SOURCE, metadata);

    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    jaxb2Marshaller.setLazyInit(true);
    jaxb2Marshaller.setContextPaths("se.lantmateriet.sercxi.gateway.outrettfastighetsomrade.config", "se.lantmateriet.geoaccess.jaxb.gml",
        "se.lantmateriet.geoaccess.outrettfastighetsomrade.jaxb");
    jaxb2Marshaller.setJaxbContextProperties(properties);

    jaxb2Marshaller.afterPropertiesSet();

    StringResult stringResult = new StringResult();

    Marshaller marshaller = jaxb2Marshaller.getJaxbContext().createMarshaller();

    marshaller.setProperty("eclipselink.media-type", MediaType.APPLICATION_JSON_VALUE);

    marshaller.marshal(result.get(0), stringResult);

    return stringResult.toString();

XML アダプタ:

public class GeoJsonAdapter extends XmlAdapter<PointPropertyType, Map<String, Object>> {

@Override
public Map<String, Object> unmarshal(final PointPropertyType v) throws Exception {
    return JTSGeometryToGeoJsonConverter.convertGeometryToJson(POSITION(N(v.getPoint().getPos().getValue().get(0)),
        E(v.getPoint().getPos().getValue().get(1))).toPoint());
}

@Override
public PointPropertyType marshal(final Map<String, Object> v) throws Exception {
    throw new UnsupportedOperationException();
}

}

そして、json出力で変換したいプロパティcentralpunktのドメインオブジェクト(このコードはサードパーティのjarファイルにあり、変更できません):

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "OutrettFastighetsomradeReferensType", namespace = "http://namespace.lantmateriet.se/distribution/produkter/outrettfastighetsomrade/v1", propOrder = {
"objektidentitet",
"fodelsenummer",
"externId",
"fastighetsomradetyp",
"centralpunkt"
})
public class OutrettFastighetsomradeReferensType {

@XmlElement(namespace = "http://namespace.lantmateriet.se/distribution/produkter/outrettfastighetsomrade/v1", required = true)
protected String objektidentitet;
@XmlElement(namespace = "http://namespace.lantmateriet.se/distribution/produkter/outrettfastighetsomrade/v1", required = true)
protected String fodelsenummer;
@XmlElement(namespace = "http://namespace.lantmateriet.se/distribution/produkter/outrettfastighetsomrade/v1", required = true)
protected String externId;
@XmlElement(namespace = "http://namespace.lantmateriet.se/distribution/produkter/outrettfastighetsomrade/v1", required = true)
protected FastighetsomradeTypType fastighetsomradetyp;
@XmlElement(name = "Centralpunkt", namespace = "http://namespace.lantmateriet.se/distribution/produkter/outrettfastighetsomrade/v1", required = true)
protected PointPropertyType centralpunkt;

しかし、それは機能していません:-(

Caused by: javax.xml.bind.JAXBException - with linked exception:
[java.lang.NullPointerException]
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:915)
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:848)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:182)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:129)
at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:137)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:108)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:258)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:412)
at org.springframework.oxm.jaxb.Jaxb2Marshaller.createJaxbContextFromContextPath(Jaxb2Marshaller.java:470)
at org.springframework.oxm.jaxb.Jaxb2Marshaller.getJaxbContext(Jaxb2Marshaller.java:443)
... 73 more
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.updateGlobalElements(AnnotationsProcessor.java:3868)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.postBuildTypeInfo(AnnotationsProcessor.java:735)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.buildNewTypeInfo(AnnotationsProcessor.java:4746)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processReferencedClass(AnnotationsProcessor.java:838)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.buildTypeInfo(AnnotationsProcessor.java:812)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.postBuildTypeInfo(AnnotationsProcessor.java:734)
at org.eclipse.persistence.jaxb.compiler.XMLProcessor.processXML(XMLProcessor.java:364)
at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:104)
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:912)
... 85 more

何か案は?

これは私が本当に欲しいものです:

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd" version="2.4">
<xml-java-type-adapters>
    <xml-java-type-adapter value="se.lantmateriet.sercxi.web.controller.GeoJsonAdapter" type="se.lantmateriet.geoaccess.jaxb.gml.PointPropertyType"/>
</xml-java-type-adapters>

しかし、それは何も影響していないようです。グローバルレベルでの xml-java-type-adapter は文書化されていないようですか?

ありがとう、

/マグナス

4

2 に答える 2

0

わかりました、ありがとう!私はほとんどそこにいます!:)

MOXy マーシャル List<?>と同じ (?) 問題があるようですが、json の結果に型が表示されるのを回避するにはどうすればよいですか? .

私のアダプターは今です(これはpojoを使用するという意味ですか?):

public class GeoJsonPointAdapter extends XmlAdapter<PointWrapper, PointPropertyType> {

    public static class PointWrapper {

        public String type;

        public Double[] coordinates;

        PointWrapper() {
            super();
        }

        public PointWrapper(final Map<String, Object> data) {
            super();
            this.type = (String) data.get("type");
            this.coordinates = (Double[]) data.get("coordinates");
        }

    }

    @Override
    public PointWrapper marshal(final PointPropertyType v) throws Exception {
        return new PointWrapper(JTSGeometryToGeoJsonConverter.convertGeometryToJson(POSITION(N(v.getPoint().getPos().getValue().get(0)),
        E(v.getPoint().getPos().getValue().get(1))).toPoint()));
    }

    @Override
    public PointPropertyType unmarshal(final PointWrapper v) throws Exception {
        throw new UnsupportedOperationException();
    }

}

テストコード:

List<Object> result = Lists.newArrayList();

    result.addAll(this.outrettFastighetsomradeReferensRepository.getByExternIdStartsWith(decodeUtf8String(externId)));

    Map<String, Object> properties = Maps.newHashMapWithExpectedSize(2);
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "json-bindings.xml");
    properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
    properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);

    JAXBContext jc = JAXBContext.newInstance(new Class[] { OutrettFastighetsomradeReferensType.class }, properties);

    Marshaller marshaller = jc.createMarshaller();

    StringResult stringResult = new StringResult();

    marshaller.marshal(result, stringResult);

    return stringResult.toString();

例外:

Caused by: Exception [EclipseLink-25007] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor for class java.util.ArrayList was not found in the project.  For JAXB, if the JAXBContext was bootstrapped using TypeMappingInfo[] you must call a marshal method that accepts TypeMappingInfo as an input parameter.
at org.eclipse.persistence.exceptions.XMLMarshalException.descriptorNotFoundInProject(XMLMarshalException.java:140)
at org.eclipse.persistence.internal.oxm.Context$ContextState.getSession(Context.java:143)
at org.eclipse.persistence.oxm.XMLContext$XMLContextState.getSession(XMLContext.java:787)
at org.eclipse.persistence.oxm.XMLContext$XMLContextState.getSession(XMLContext.java:1)
at org.eclipse.persistence.internal.oxm.Context.getSession(Context.java:451)
at org.eclipse.persistence.oxm.XMLContext.getSession(XMLContext.java:356)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:142)
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:487)
... 73 more

リストから 1 つの項目をマーシャリングするように変更すると、次のようになります。

marshaller.marshal(result.get(0), stringResult);

それはすべて動作します。どうしたの?

于 2013-11-15T11:08:11.023 に答える