球衣的 EntityHolder 类型的层次结构

发布于 2024-11-10 11:31:23 字数 1172 浏览 3 评论 0原文

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2024-11-17 11:31:23

JAXB 注释存在问题:InteractionRequestInteractionChatRequest 均使用 @XmlRootElement(name = "request") 进行注释。因此它们具有相同的根元素,这使得 JAXB 无法区分它们。

尝试将 InteractionChatRequest 更改为 @XmlRootElement(name = "chat-request")

You have a problem with the JAXB annotations: both InteractionRequest and InteractionChatRequest 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").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文