我們正在使用Jersey實現RESTful API,利用其自動WADL生成功能。將@PathParam的可能值包含到WADL中
只是作爲一個例子,我們有方法
@GET
@Path("/{id}/{attribute}")
@Produces(MediaType.APPLICATION_JSON)
public Object getAttributeByID(@PathParam("id") long id, @PathParam("attribute") String attribute) {
....
}
這產生在WADL以下片段:
<param type="xs:string" style="template" name="attribute:.*"/>
屬性可以是name
,type
,size
,我們希望不僅要驗證的值在運行時還要顯示在生成的瓦片中根據this document這樣的特性應該可以通過在<param>
內產生幾個標籤<option>
來支持,我.e。我期待如下內容:
<param type="aws:Attributes" style="template" name="attribute">
<option value="name"/>
<option value="type"/>
<option value="size"/>
</param>
我的問題是啓用它與澤西島。如果未能找到相關文件,並認爲可能是,如果我改變從String
到enum
參數的類型,這個功能會自動工作,所以我改變了方法簽名:
@Path("/{id}/{attribute}")
@Produces(MediaType.APPLICATION_JSON)
public Object getAttributeByID(@PathParam("id") long id, @PathParam("attribute") Attribute attribute) {
....
}
其中
public enum Attribute {
name, type, size
}
但澤西島仍然生成<param>
標籤,但沒有選項,參數類型仍爲xs:string
。
我試圖在澤西島的代碼中找到它,發現帶有相關JAXB註釋的類com.sun.research.ws.wadl.Option
,所以它似乎是相關的,但我不知道如何使它工作。我想這個問題在WadlGeneratorConfig
。
這裏是澤西定義的相關部分在我們web.xml
<filter>
<filter-name>REST-API</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
................
<init-param>
<param-name>com.sun.jersey.config.property.WadlGeneratorConfig</param-name>
<param-value>com.mycompany.resource.OurWADLGenerator</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mycompany</param-value>
</init-param>
</filter>
其中OurWADLGenerator
代碼:
public class OurWADLGenerator extends WadlGeneratorConfig {
@Override
public List<WadlGeneratorDescription> configure() {
return generator(WadlGeneratorApplicationDoc.class)
.prop("applicationDocsStream", "application-doc.xml")
.generator(WadlGeneratorResourceDocSupport.class)
.prop("resourceDocStream", "resourcedoc.xml").descriptions();
}
}
缺少什麼我在這裏? 在此先感謝。
'公共枚舉Attribure'應該是'public enum Attribute' ...是從你的代碼複製還是隻是一個錯字? – 2015-04-24 09:15:13
@martinjakubik,肯定是錯字。謝謝。我剛剛編輯了這個帖子並修復了這個錯誤。 – AlexR 2015-04-25 10:49:06