Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to remove the class attribute when serializing Java Bean to xml? #333

Closed
cmanlh opened this issue Apr 10, 2023 · 1 comment
Closed
Assignees
Labels

Comments

@cmanlh
Copy link

cmanlh commented Apr 10, 2023

Have a class with generic type property as below:

@XStreamAlias("ipmp")
public class Ipmp<T> implements Serializable {
    private static final long serialVersionUID = -2075754377918779208L;

    private Head head;

    @XStreamAlias("body")
    private T body;

    public Head getHead() {
        return head;
    }

    public Ipmp setHead(Head head) {
        this.head = head;

        return this;
    }

    public T getBody() {
        return body;
    }

    public Ipmp setBody(T body) {
        this.body = body;

        return this;
    }
}

Do generating

XStream xStream = new XStream();
        xStream.processAnnotations(Ipmp.class);

        Ipmp ipmp = new Ipmp<BalanceInfoQuery>();

        Head head = new Head();
        head.setBusiCode("10002").setReference("req".concat(String.valueOf(System.currentTimeMillis())));
        ipmp.setHead(head);

        BalanceInfoQuery balanceInfoQuery = new BalanceInfoQuery();
        balanceInfoQuery.setAccount("01").setAccountType("02").setBankCode("03").setCurrency("USD").setDate("20230410").setQueryMode("00").setSubmitWay("00");
        ipmp.setBody(balanceInfoQuery);

        System.out.println(xStream.toXML(ipmp));

The result as below :

<ipmp>
<head>
<reference>req1681112722751</reference>
<busiCode>10002</busiCode>
</head>
<body class="xx.xx.bean.BalanceInfoQuery">
<queryMode>00</queryMode>
<bankCode>03</bankCode>
<account>01</account>
<accountType>02</accountType>
<date>20230410</date>
<currency>USD</currency>
<submitWay>00</submitWay>
</body>
</ipmp>

The body node has a attribute named class. I guess generating the class attribute for deserializing. But we don't need deserializing usring Java. The class attribute will cause issue in the next using case.

How to remove it? Does there any configuation working on it? Thanks

@joehni joehni self-assigned this Apr 22, 2023
@joehni
Copy link
Member

joehni commented Apr 22, 2023

You can set the system attribute class to null:

xstream.aliasSystemAttribute(null, "class");

System attribute: An attribute XStream writes on its own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants