问题描述
我正在尝试为我的一个对象创建一个 XMLAdapter
类.我需要从另一个类访问 Getters
,以便可以从该类的Getters中填充某些对象,但我无法这样做.
I am trying to create an XMLAdapter
class for one of my objects. I need to access Getters
from another class so that some of the objects can be populated from that class's Getters` but I am unable to do so.
基本上,我想在 XMLAdapter
中访问我的 Child
类 Getter
方法.我可以创建 Child
类的对象并进行访问,但是会导致 NULL
.最初,我在 Child
和 Parent
类中填充 Objects
.我想在 XML适配器
中访问相同的值,所以我想知道是否有一种方法可以将类的引用传递给我的 XMLAdapter
.
Basically, I would like to access my Child
class Getter
methods within the XMLAdapter
. I can create an Object of the Child
class and access but it results in NULL
. Initially, I am populating the Objects
in the Child
and Parent
class. I would like to access the same value within XML Adapter
so I am wondering if there is a way to pass reference of a class to my XMLAdapter
.
我有以下 Parent
类,该类将具有值:
I have the following Parent
class which will have the values:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
@XmlSeeAlso({Child.class})
public class Parent{
private String eventID;
}
以下是我的 Child
类,将在编组期间使用该类创建 XML
:
Following is my Child
class which will be used during marshalling to create XML
:
@XmlRootElement(name = "child")
@XmlType(name = "child", propOrder = { "name","extension"})
public class Child extends Parent
{
@XmlElement (name = "name")
private String name;
@JsonIgnore
@XmlJavaTypeAdapter (ExtensionAdapter.class)
@XmlElement (name = "extension")
private Extension extension;
}
以下是我的扩展程序类:
Following is my Extension class:
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Extension {
private String eventID;
}
以下是我的 XMLAdapter
类:
public class ExtensionAdapter extends XmlAdapter<Child, Extension> {
@Override
public Extension unmarshal(Child valueType) throws Exception {
System.out.println("UN-MARSHALLING");
return null;
}
@Override
public Child marshal(Extension boundType) throws Exception {
System.out.println("MARSHALLING");
System.out.println(boundType);
//If I create the New child type then I am unable to access the eventID from its getter as Child class is nulll
Child be = new Child();
be.setEventID(boundType.getEventID());
return be;
}
}
基本上,我想访问 XMLAdapter
中的 Child
glass Getter
方法,以便我可以填充 Extension
类 EventID
.我尝试了很多事情,但没有任何效果,所以我在这里发布.
Basically, I would like to access the Child
glass Getter
methods within the XMLAdapter
so that I can populate my Extension
class EventID
with that. I tried a lot of things but nothing worked so I am posting here.
有没有一种方法可以将我的 Child
类作为参数传递给 XMLAdapter
,以便可以从中访问 Getter
方法?因为如果我创建一个 Child
类的新对象,则其所有 Getter
方法都是空的.但是,从一开始,我就填充了 Parent
和 Child
类中的所有字段(名称和事件ID).我只想使用 XMLAdapter
在 Extension
类 eventID
中填充相同的值.
Is there a way I can pass my Child
class as a parameter to XMLAdapter
so that I can access the Getter
methods from it? Because if I create a new Object of Child
class then all of its Getter
methods are null. However at the start I am populating all the fields in Parent
and Child
class (name and eventID). I just want to populate the same value within the Extension
class eventID
using the XMLAdapter
.
我希望我能够解释这个需求.如果您有任何建议或示例代码,那将非常有用.
I hope I was able to explain the need. If you have any suggestions or sample code then it would be really useful.
请注意:我在这里使用了非常简单的代码.我知道可以使用适当的POJO和JAXB注释来实现我想达到的目标,但是我的实际类很复杂,不允许修改它.我所能添加的就是新的注释.因此,我正在尝试使用 XMLAdapter
来实现这一目标.
Please Note: I have used very sample code here. I am aware that whatever I am trying to achieve can be achieved using the proper POJO and JAXB Annotations but my actual class is complex and I am not allowed to modify it. All I can add is the new Annotations. Hence, I am trying to achive this using the XMLAdapter
.
推荐答案
我想实现类似的功能,因此无需访问该类就可以做到这一点.发布答案,以便将来对某人有用.
I wanted to achieve something like this so I was able to do that without needing to access the class. Posting the answer so it can be useful to somebody in the future.
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang3.NotImplementedException;
import io.openepcis.model.jaxb.MyAdapter.MapWrapper;
public class MyAdapter extends XmlAdapter<MapWrapper, Map<String, Object>> {
private DocumentBuilder documentBuilder;
public MyAdapter() throws Exception {
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
@Override
public Map<String, Object> unmarshal(MapWrapper valurType) throws Exception {
throw new NotImplementedException();
}
@SuppressWarnings("unchecked")
@Override
public MapWrapper marshal(Map<String, Object> m) throws Exception {
if (m == null) {
return null;
}
MapWrapper wrapper = new MapWrapper();
List elements = new ArrayList();
for (Map.Entry<String, Object> property : m.entrySet()) {
if (property.getValue() instanceof Map) {
elements.add(new JAXBElement<MapWrapper>(new QName(getCleanLabel(property.getKey())), MapWrapper.class, marshal((Map) property
.getValue())));
} else {
elements.add(new JAXBElement<String>(new QName(getCleanLabel(property.getKey())), String.class, property.getValue().toString()));
}
}
wrapper.elements = elements;
return wrapper;
}
// Return a XML-safe attribute. Might want to add camel case support
private String getCleanLabel(String attributeLabel) {
attributeLabel = attributeLabel.replaceAll("[()]", "").replaceAll("[^\\w\\s]", "_").replaceAll(" ", "_");
return attributeLabel;
}
public static class MapWrapper {
@XmlAnyElement
List elements;
}
这篇关于有没有一种方法可以将Class作为参数传递给JAXB XMLAdapter或从另一个类访问Getter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!