我正在尝试做与Can I get MOXy to convert a string to a boolean when generating json几乎相同的事情。
我从xml开始 - >宾语。对象现在包含我想要输出为GeoJson的几何。
我试图在我的xml-bindings.xml中添加xml-java-type-adapters,但它们似乎永远不会运行。 package-info.java中的Annoations运行,但会影响xml->对象也是,而不仅仅是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();
XMLAdapter:
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似乎没有文档?
谢谢,
/马格努斯
答案 0 :(得分:0)
有一些问题在起作用。以下是我第一次尝试答案:
XmlAdapter
由于您正在尝试调整PointPropertyType
课程,因此您的XmlAdapter
会向后调整。而不是这个。
public class GeoJsonAdapter extends XmlAdapter<PointPropertyType, Map<String, Object>> {
你应该:
public class GeoJsonAdapter extends XmlAdapter<Map<String, Object>, PointPropertyType> {
XmlAdapter
目前使用Map
作为XmlAdapter
中的值类型存在问题(见下文)。现在你应该使用普通的POJO。
MOXy元数据(json-bindings.xml)
当您通过JAXB注释或MOXy的元数据文档在包级别指定XmlAdapter
时,它将应用于属于该包中的类的所有映射字段/属性。在下面的示例文档中,se.lantmateriet.sercxi.web.controller.GeoJsonAdapter
XmlAdapter将应用于属于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>
了解更多信息
下面是一个有助于简化引导过程的代码示例。如果以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);
答案 1 :(得分:0)
好的,谢谢!我快到了! :)
我似乎与MOXy marshal List<?> how to avoid that it shows the type in json results?有相同的(?)问题。
我的适配器现在(这是你用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);
一切正常。问题是什么?