Using JAXB to pass subclass instances as superclass

后端 未结 1 1143
梦如初夏
梦如初夏 2021-01-04 17:47

What I have is a set of Java classes (close to 25) representing message types. They all inherit from a Message class which I\'d like to be abstract. Each message type adds

相关标签:
1条回答
  • 2021-01-04 18:12

    Have you tried adding the following to your message class? The @XmlSeeAlso annotation will let the JAXBContext know about the subclasses.

    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlSeeAlso;
    
    @XmlRootElement
    @XmlSeeAlso(RegMessage.class)
    public abstract class Message {
    
        Integer id;
    
    }
    

    Alternate Strategy:

    Here is a link to a strategy I have helped people use:

    • http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html

    Essentially you have one message object, and multiple individual message payloads. The relationship between the message and payload is handled through a @XmlAnyElement annotation.

    Note on Transaction Handling

    I noticed that you are handling your own transactions. Have you considered implementing your JAX-RS service as a session bean and leverage JTA for your transaction handling? For an example see:

    • http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-45.html
    0 讨论(0)
提交回复
热议问题