我想将对象封送到XML。
但是,除以下情况外,它失败了:
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: unable to marshal type "FreightOfferDetail" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:331)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:257)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96)
at com.wktransportservices.fx.test.util.jaxb.xmltransformer.ObjectTransformer.toXML(ObjectTransformer.java:27)
at com.wktransportservices.fx.test.sampler.webservice.connect.FreightOfferToConnectFreight.runTest(FreightOfferToConnectFreight.java:59)
at org.apache.jmeter.protocol.java.sampler.JavaSampler.sample(JavaSampler.java:191)
at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:429)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257)
at java.lang.Thread.run(Thread.java:662)
Caused by: com.sun.istack.SAXException2: unable to marshal type "FreightOfferDetail" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:244)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:303)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:490)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
事实上,这个注释是存在的(对于父类和交付的类):
@XmlRootElement(name = "Freight_Offer")
@XmlAccessorType(XmlAccessType.FIELD)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class FreightOffer {
@JsonIgnore
@XmlTransient
private String freightId;
private String id;
private String externalSystemId;
private AddressLocation pickUp;
private AddressLocation delivery;
private FreightDescription freightDescription;
private ListContacts contacts;
private Customer customer;
private ListSla slas;
private String pushId;
private CompanyProfile company;
private Route route;
private String href;
private Lifecycle lifecycle;
private Visibility visibility;
private Boolean unfoldedVXMatching;
// getters / setters
儿童班:
@XmlAccessorType(XmlAccessType.PROPERTY)
public class FreightOfferDetail extends FreightOffer {
private List<Contact> contact;
@XmlElement(name = "contacts")
@JsonProperty("contacts")
public List<Contact> getContact() {
return contact;
}
public void setContact(List<Contact> contact) {
this.contact = contact;
}
它在该方法中完全失败:
public class ObjectTransformer<T> implements Transformer<T> {
protected final JAXBContext context;
protected final Marshaller marshaller;
protected final int okStatusCode = 200;
protected final String okSubErrorCode = "OK";
public ObjectTransformer(JAXBContext context) throws JAXBException {
this.context = context;
marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.encoding", "UTF-8");
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
}
public String toXML(T object) throws JAXBException {
StringWriter writer = new StringWriter();
marshaller.marshal(object, writer);
String xmlOffer = writer.toString();
return xmlOffer;
}
它应该起作用,但不应该。
我找不到这里遗漏或错误的地方。
更新:
以下是测试片段:
public SampleResult runTest(JavaSamplerContext context) {
AbstractSamplerResults results = new XMLSamplerResults(new SampleResult());
results.startAndPauseSampler();
if (failureCause != null) {
results.setExceptionFailure("FAILED TO INSTANTIATE connectTransformer", failureCause);
} else {
FreightOfferDTO offer = null;
FreightOffer freightOffer = null;
try {
results.resumeSampler();
RouteInfo routeDTO = SamplerUtils.getRandomRouteFromRepo(context.getIntParameter(ROUTES_TOUSE_KEY));
offer = FreightProvider.createRandomFreight(routeDTO, createUserWithLoginOnly(context));
freightOffer = connectTransformer.fromDTO(offer);
String xmlOfferString = connectTransformer.toXML(freightOffer); // <- it fails here.
我从CSV文件中获取日期,并将其转换为DTO对象。此方法返回给我FreightOfferDetail
以下是此方法的片段:
public FreightOfferDetail freightFromDTO(FreightOfferDTO freightDTO, boolean fullFormat){
FreightOfferDetail freight = new FreightOfferDetail();
freight.setFreightId(freightDTO.getIds().getAtosId());
freight.setId(freightDTO.getIds().getFxId());
// ...
在这种情况下,如何将对象封送到XML文件?
您可以使用ObjectFactory类来解决没有@XmlRootElement的类。ObjectFactory具有重载方法。
方法:1它简单地创建对象和
方法:2它将用@JAXBElement包装对象。
总是使用方法2来避免javax。xml。绑定MarshalException-链接异常缺少@XmlRootElement注释
方法:1
public GetCountry createGetCountry() {
return new GetCountry();
}
方法:2
@XmlElementDecl(namespace = "my/name/space", name = "getCountry")
public JAXBElement<GetCountry> createGetCountry(GetCountry value) {
return new JAXBElement<GetCountry>(_GetCountry_QNAME, GetCountry.class, null, value);
}
有关更多信息,请遵循此stackoverflow问题
希望这会有帮助。。。
请注意,错误消息涉及的是FreightOfferDetail,而不是FreightOffer。
基于此,我希望在某个地方(在提供的代码之外),您需要整理一个细节。
如果您希望能够做到这一点(使用JAXB?)您还需要使用XmlRootElement注释对该类进行注释。
public String toXML(T object) throws JAXBException {
StringWriter stringWriter = new StringWriter();
JAXBContext jaxbContext = JAXBContext.newInstance(T.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// format the XML output
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
QName qName = new QName("com.yourModel.t", "object");
JAXBElement<T> root = new JAXBElement<Bbb>(qName, T.class, object);
jaxbMarshaller.marshal(root, stringWriter);
String result = stringWriter.toString();
LOGGER.info(result);
return result;
}
当我必须在没有@XmlRootElement的情况下封送/解封送时,我会使用以下文章:http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html
希望它能有所帮助:)
我需要对照我的模式来验证Class object,在我的模式中,我提供了正则表达式来验证自动生成的JAXB类的字段。当我尝试验证我的类对象时,我得到以下错误: 我还有别的办法可以解决吗?
我有以下JAXB对象: 我使用JAXB编组程序将对象转换为XML: 它运行平稳,没有任何异常,但是,结果总是缺少元素键。我试图将其改为Attribute,但它也不起作用。以下是输出示例: 我曾尝试将键更改为另一个名称,例如AuthKey,但它仍然缺失。我做错了什么? 编辑我发现了问题,这是因为我的键的值是空的。如果它包含值,则将其写入XML。然而,您能解释一下为什么在XMLElement中,我写了
我得到的错误是:无法将java.lang.String实例反序列化出START_OBJECT令牌 如何将xml元素反序列化更正为基元类型?
问题内容: 我尝试在本地运行wampserver,但mysql服务器未运行。当我尝试 安装服务时 ,它给我错误。我整天都在搜索答案,并在这里和那里找到了一些答案。 但任何解决方案都不适合我。我试图在Windows7家庭OS vmware上安装warpserver 对我有帮助吗? 问题答案: 我终于找到解决问题的方法。我已经尝试了几种方法来解决上述问题,但对我来说却无济于事。 如您所知,WampSe
null 问候
你好,我正在为三星Gear创建一个小工具。我用Tizen IDE做可穿戴设备。我关注了他们的youtube tutoria:如何创建基本的集成Gear应用程序。 我尝试在Tizen IDE中构建项目,但出现了一个构建错误: 生成过程中出错。对项目“SimpleSAPConsumer”运行生成器“Web小部件生成器”时出错。构建错误:无法确定图形元素类型,因为文档类为空。这可能是一个投影,请使用EX