繁体   English   中英

CXF JAXRS | 生成的wadl中不存在复杂响应类型

[英]CXF JAXRS | Complex response types are not present in the generated wadl

我们使用cxf 2.5.2和spring来暴露和消费宁静的服务。 为了分发服务接口类,我们开始使用wadl2java目标(根据给定的wadl文件生成接口类)

生成的wadl不包含正确的响应类型,因为我猜测,生成的接口都有'Response'作为返回类型。

防爆。 如果restful get方法返回'List',则生成的wadl仅包含以下段:

<response><representation mediaType="application/json"/></response>

并且从该wadl文件生成的相应接口包含返回类型为“Response”

有人可以建议需要做些什么来防止实际的响应类型丢失吗? 是否有任何注释(如ElementClass?如何使用它?)或提供者需要?

当前代码:

@GET
@Path("/itemsForCategory")
@Produces("application/json")
@Description("getItemsForCategory")
public List<Item> getItemsForCategory(@QueryParam("category")String category) {

通用的“响应”返回类型似乎与您尝试返回列表的事实无关。 也就是说,即使使用“Item”作为返回类型,也会导致生成的界面中的方法返回类型为“Response”。 要解决此问题,您需要在WADL资源响应中添加element属性:

<response><representation mediaType="application/json" element="item"/></response>

如果直接修改WADL,则可以使用,可以支持也可以不支持等效的JAX-RS注释。 这也无法解决返回列表的问题。 我的建议(我之前使用过)是创建一个封装List返回类型的包装列表类型(例如ItemList)。

在任何一种情况下,您都需要从底部向上翻转到自上而下(即WADL优先)实现。 这应该不会太糟糕,因为你已经有了实现,你可以让它实现生成的接口。

为了澄清这一切,我基于标准的JAX-RS“Bookstore”示例制作了一个简单的示例项目。 您可以在github上查看pom (使用wadl2java配置)和实际的wadl 生成的代码也在那里(例如, BookstoreidResource.java )。

在处理列表,映射等时,我遇到了类似的问题。因为在生成WSDL时集合在运行时不知道它们的类型,所以忽略了放入集合的类型。 我发现,例外情况是,另一个Web服务公开的方法使用了该特定类型。 作为一种解决方法,我创建了一个虚拟方法,它使用了列表和地图所需的每种类型。

因此,例如,我有一个名为User的类,它扩展了一个名为BaseObject的抽象类,该类未被Web服务直接使用。 但是,有时在搜索用户时会通过列表。 以下代码是我的解决方法。

@WebService
public interface MyService
{
    // Various @WebMethods here

    /**
     * This method should not be used. This is a workaround to ensure that
     * User is known to the JAXB context. Otherwise you will get exceptions like this:
     * javax.xml.bind.JAXBException: class java.util.User nor any of its super class is known to this context.
     * Or it will assume that using BaseObject is OK and deserialisation will fail
     * since BaseObject is abstract.
     * This issue occurs because the classes available to the JAXB context
     * are loaded when the endpoint is published. At that time it is not known
     * that User will be needed since it is not explicitly referenced
     * in any of these methods. Adding user here will cause it to be added to
     * the context.
     * @param user
     * @return
     */
    @WebMethod
    void dummy(@WebParam(name="user") User user);
}

我承认这是一个讨厌的工作,我不认为这是一个正确的解决方案,但也许它会让你继续前进,直到有人能提供更好的解决方案。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM