簡體   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