球衣的 EntityHolder 类型的层次结构
@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
@JSONConfigurable
public class InteractionRequest {
@XmlElement(name = "skill")
protected String skillName;
}
@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
@JSONConfigurable
public class InteractionChatRequest extends InteractionRequest {
@XmlElement
@XmlJavaTypeAdapter(LPStringsXmlAdapter.class)
@XmlElementWrapper(name = "preChatLines")
protected List<String> line;
}
和 2 次用法:
@PUT
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response postExitSurvey(EntityHolder<InteractionRequest> ent) {
InteractionRequest request = ent.getEntity();
return null;
}
@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response interactionRequest(EntityHolder<InteractionChatRequest> ent) {
InteractionChatRequest params = ent.getEntity();
return null;
}
现在,在这两种情况下,实体持有者都持有 InterationRequest 对象,这会在第二次用法中导致 ClassCastException。
知道为什么吗? Jersey 不应该将实体转换为我声明的类型吗? 在这种情况下,等级制度还可能吗?
谢谢, 乌迪
@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
@JSONConfigurable
public class InteractionRequest {
@XmlElement(name = "skill")
protected String skillName;
}
@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
@JSONConfigurable
public class InteractionChatRequest extends InteractionRequest {
@XmlElement
@XmlJavaTypeAdapter(LPStringsXmlAdapter.class)
@XmlElementWrapper(name = "preChatLines")
protected List<String> line;
}
And 2 usages:
@PUT
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response postExitSurvey(EntityHolder<InteractionRequest> ent) {
InteractionRequest request = ent.getEntity();
return null;
}
@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response interactionRequest(EntityHolder<InteractionChatRequest> ent) {
InteractionChatRequest params = ent.getEntity();
return null;
}
Now, in both cases, the entity holder holds InterationRequest object which results in a ClassCastException in the second usage.
Any idea why? Shouldn't Jersey cast the entity to the type I declare?
Is hierarchy even possible in this case?
Thanks,
Udi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JAXB 注释存在问题:
InteractionRequest
和InteractionChatRequest
均使用@XmlRootElement(name = "request")
进行注释。因此它们具有相同的根元素,这使得 JAXB 无法区分它们。尝试将
InteractionChatRequest
更改为@XmlRootElement(name = "chat-request")
。You have a problem with the JAXB annotations: both
InteractionRequest
andInteractionChatRequest
are annotated with@XmlRootElement(name = "request")
. So they have the same root element, which makes it impossible for JAXB to distinguish between them.Try to change the
InteractionChatRequest
to@XmlRootElement(name = "chat-request")
.