簡體   English   中英

如何在返回Response時在Jersey WADL中包含類型

[英]How do I include types in Jersey WADL while also returning Response

情況

我有一個返回User對象的Jersey 2.18 API端點。 我的利益相關者需要API來生成WADL文件,該文件不僅反映API路徑,還反映返回的對象類型。

通過使用以下端點定義,截至2015年,澤西開箱即用:

@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public User getExampleUser() {
    User exampleUser = new User();
    return exampleUser;
}

由jersey生成的WADL文件正確包含端點以及返回類型:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
    <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.18 2015-06-05 02:28:21"/>
    <doc xmlns:jersey="http://jersey.java.net/" jersey:hint="This is simplified WADL with user and core resources only. To get full WADL with extended resources use the query parameter detail. Link: http://localhost:8080/example/api/v3/application.wadl?detail=true"/>
    <grammars>
        <include href="application.wadl/xsd0.xsd">
            <doc title="Generated" xml:lang="en"/>
        </include>
    </grammars>
    <resources base="http://localhost:8080/example/api/v3/">
        <resource path="/">
            <method id="getExampleUser" name="GET">
                <request>
                </request>
                <response>
                    <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="user" mediaType="application/json"/>
                    <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="user" mediaType="application/xml"/>
                </response>
            </method>
        </resource>    
    </resources>
</application>

但是大多數Jersey社區似乎都有端點返回一個更通用的Response對象,它允許各種各樣的好東西,包括E-TAG緩存 ,HTTP狀態代碼操作, 錯誤消息等等。

例如:

@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getExampleUser()  {
    User exampleUser = new User();
    return Response.ok(exampleUser).build();
}

生成的WADL看起來相同,但響應部分現在沒有顯示返回類型和模式的證據。

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

我的問題

是否可以從豐富的自動生成的WADL文件中受益,同時還能夠讓我的端點返回更靈活的Response對象?

或者,如何在仍然從端點定義返回特定對象類型的同時處理重定向,緩存和其他基本API功能?

我有完全相同的問題,並能夠像這樣解決它:

  • 我的所有實體都必須使用@XmlRootElement進行注釋,並將其發送到虛擬休息端點,以便它們顯示在application.wadl/xsd0.xsd
  • 我在項目中包含了Swagger ,並將其用於生成包含響應代碼和響應對象的文檔。
  • 然后我運行了現有的wadl生成工具application.wadl
  • 最后一步是編寫一個方法,將Swagger文件中的響應代碼和對象插入到wadl文件中。

最后三個步驟放在一個休息服務中。 結果是通過調用其余服務自動生成wadl,您可以像以前一樣繼續使用Response

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM