我正在尝试做与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 似乎没有记录?
谢谢,
/马格纳斯