使用JAXB的ArrayList类型的Marshall自定义对象

时间:2017-12-14 17:08:22

标签: java xml arraylist jaxb marshalling

我正在使用JAXB封送ArrayList类型的Object。我已经创建了一个Wrapper类,因为我们不能直接Marshal ArrayList,但我仍然遇到错误。

要编组的类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class FindTransactionDto implements IFindTransactionDto {

private String agencyCd;
private String createdDate;
private String custEmail;
private String custPhone;

包装类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class FindTransactionWrapper {

@XmlElementWrapper
@XmlElement(required = true)
private List<FindTransactionDto> findTransactions;

public FindTransactionWrapper() {
    findTransactions = new ArrayList<>();
}

// getters/setters

JAXB Marshaling Logic

private String processObjectToXML( Object object, String serviceName, String operationType ) throws Exception {
    JAXBContext context = JAXBContext.newInstance(FindTransactionWrapper.class );

    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );

    StringWriter writer = new StringWriter();
    marshaller.marshal( object, writer );

    return writer.toString();
}

堆栈跟踪

javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.

0 个答案:

没有答案