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

cra*_*rty 5 java rest cxf jax-rs 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) {
Run Code Online (Sandbox Code Playgroud)

jsa*_*ven -1

我在处理列表、映射等时遇到了类似的问题。因为在生成 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);
}
Run Code Online (Sandbox Code Playgroud)

我承认这是一个有点令人讨厌的解决方案,并且我不认为这是一个正确的解决方案,但也许它会让您继续前进,直到有人可以提供更好的解决方案。

希望这可以帮助。