gpt4 book ai didi

java - 无法将类型编码为 XML 元素,因为缺少 @XmlRootElement 注释

转载 作者:数据小太阳 更新时间:2023-10-29 02:00:43 27 4
gpt4 key购买 nike

我想将对象编码为 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;
}

正是在这个方法 toXML() 上失败了:

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 文件?

最佳答案

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

祝一切顺利:)

关于java - 无法将类型编码为 XML 元素,因为缺少 @XmlRootElement 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37050316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com