java xmlelement 无视,java – 无法将类型编组为XML元素,因为缺少@XmlRootElement注释

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

@XmlElement(name = "contacts")

@JsonProperty("contacts")

public List getContact() {

return contact;

}

public void setContact(List contact) {

this.contact = contact;

}

它完全失败了这个方法toXML():

public class ObjectTransformer implements Transformer {

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); //

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值