JAXB JSON注释类(Jackson),包含抽象属性



使用以下类,它们构成了我在JAX-RS调用(CXF)后传输到客户端的JSON消息结构,我接收

org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem with reading the response message, class : class bg.vivacom.sel.dto.SELResponse, ContentType : application/json.
...
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of bg.vivacom.sel.dto.SELDTO, problem: abstract types can only be instantiated with additional type information
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@16edbe39; line: 1, column: 157] (through reference chain: bg.vivacom.sel.dto.SELResponse["dto"])

返回的对象包含属性dto,它是一个接口

@XmlRootElement(name = "SELResponse")
public class SELResponse implements Serializable{
@XmlElement(name = "corelationID")
private String corelationID;
@XmlElement(name = "responseTimeStamp")  
private String responseTimeStamp;
@XmlElement(name = "respStatusCode")  
private String respStatusCode;
@XmlElement(name = "respStatusMessage")  
private String respStatusMessage;
@XmlElement(name = "processStatus")  
private ProcessStatus processStatus;
@XmlElement(name = "dto")  
private SELDTO dto; 

接口

public interface SELDTO extends Serializable {

是一种根据请求在我的回答中包括许多不同DTO的方法,例如

@XmlRootElement(name = "customerProfile")
public class CustomerProfileDTO implements SELDTO {
@XmlElement(name = "customerCode")
private String customerCode;
...

@XmlRootElement(name = "sms")
public class SmsDTO implements SELDTO {
@XmlElement(name = "From")
private String from;
...

任何其他想法,我必须对类进行注释,以便将响应正确设置为特定的对象类型。我知道它需要额外的信息,因为在它重新创建dto对象时,它不知道它的类型,所以我尝试对接口进行如下注释:

@XmlSeeAlso({CustomerProfileDTO.class, SmsDTO .class})
public interface SELDTO extends Serializable {

但我仍然明白这个问题。任何想法都将不胜感激。

我建议在这种情况下使用Jackson注释'@JsonTypeInfo'。

但是,如果必须使用JAXB注释,请使用@XmlElements@XmlElementRefs,类似于将它们与JAXB/XML:一起使用

     @XmlElements({
             @XmlElement(type=SubClass1.class, name="sub1"),
             @XmlElement(type=SubClass2.class, name="sub2")
     })

请注意,您必须在此处包含到可能的子类型的特定映射。

最新更新