1

我正在尝试做与Can I get MOXy to convert a string to a boolean when generated json几乎相同的事情。

我从 xml -> 对象开始。对象现在包含我想要输出为 GeoJson 的几何图形。

我试图在我的 xml-bindings.xml 中添加 xml-java-type-adapter,但它们似乎从未运行。package-info.java 中的注解会运行,但也会影响 xml-> object,而不仅仅是 object->json。这就是为什么我认为 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 marshal 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

如果我更改为编组列表中的一项:

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

一切正常。有什么问题?

于 2013-11-15T11:08:11.023 回答
0

有几个问题在起作用。以下是我第一次尝试答案:


创建XmlAdapter

由于您正在尝试适应PointPropertyType课程,因此您XmlAdapter倒退了。而不是这个。

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

你应该有:

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

映射作为值类型XmlAdapter

目前在(见下文)中使用 aMap作为值类型存在问题。XmlAdapter现在,您应该改用常规 POJO。


指定包级适配器

MOXy 元数据 (json-bindings.xml)

当您XmlAdapter通过 JAXB 注释或 MOXy 的元数据文档在包级别指定时,它将应用于属于该包中的类的所有映射字段/属性。在下面的示例文档中,se.lantmateriet.sercxi.web.controller.GeoJsonAdapterXmlAdapter 将应用于se.lantmateriet.geoaccess.jaxb.gml.PointPropertyType属于forum19952922包中类的类型的字段/属性。

<?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"
    package-name="forum19952922">
        <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-bindings>

了解更多信息


简化引导程序

下面是一个有助于简化引导过程的代码示例。如果作为 a 给出的位置String与将加载该文件的相对文件位置匹配,则将尝试将该位置作为类路径上的资源。

    Map<String, Object> properties = new HashMap<String, Object>(2);
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum19952922/json-bindings.xml");
    properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
    JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);
于 2013-11-13T21:41:50.040 回答