2013-02-21 26 views
8

我們正在使用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:.*"/> 

屬性可以是nametypesize,我們希望不僅要驗證的值在運行時還要顯示在生成的瓦片中根據this document這樣的特性應該可以通過在<param>內產生幾個標籤<option>來支持,我.e。我期待如下內容:

<param type="aws:Attributes" style="template" name="attribute"> 
    <option value="name"/> 
    <option value="type"/> 
    <option value="size"/> 
</param> 

我的問題是啓用它與澤西島。如果未能找到相關文件,並認爲可能是,如果我改變從Stringenum參數的類型,這個功能會自動工作,所以我改變了方法簽名:

@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(); 
    } 
} 

缺少什麼我在這裏? 在此先感謝。

+0

'公共枚舉Attribure'應該是'public enum Attribute' ...是從你的代碼複製還是隻是一個錯字? – 2015-04-24 09:15:13

+0

@martinjakubik,肯定是錯字。謝謝。我剛剛編輯了這個帖子並修復了這個錯誤。 – AlexR 2015-04-25 10:49:06

回答

2

一些調查後,我沒有找到球衣,其中填充選項列表中的任何代碼。(可能的東西是還不支持

所以,你可以實現自己的WadlGenerator並將其插入發電機鏈。

以下是添加了<option>元素類型的參數樣本OptionsWadlGeneratorEnum

package com.mycompany; 

import com.sun.jersey.api.model.AbstractMethod; 
import com.sun.jersey.api.model.AbstractResource; 
import com.sun.jersey.api.model.AbstractResourceMethod; 
import com.sun.jersey.api.model.Parameter; 
import com.sun.jersey.server.wadl.WadlGenerator; 
import com.sun.research.ws.wadl.Application; 
import com.sun.research.ws.wadl.Method; 
import com.sun.research.ws.wadl.ObjectFactory; 
import com.sun.research.ws.wadl.Option; 
import com.sun.research.ws.wadl.Param; 
import com.sun.research.ws.wadl.RepresentationType; 
import com.sun.research.ws.wadl.Request; 
import com.sun.research.ws.wadl.Resource; 
import com.sun.research.ws.wadl.Resources; 
import com.sun.research.ws.wadl.Response; 

import javax.ws.rs.core.MediaType; 

public class OptionsWadlGenerator implements WadlGenerator { 

    private WadlGenerator _delegate; 

    private ObjectFactory objectFactory = new ObjectFactory(); 

    @Override 
    public Param createParam(AbstractResource r, AbstractMethod m, Parameter p) { 
     Param param = _delegate.createParam(r, m, p); 
     if(((Parameter)p).getParameterClass().isEnum()){ 
      Object[] values = p.getParameterClass().getEnumConstants(); 
      for(Object enumItem:values){ 
       Option option = objectFactory.createOption(); 
       option.setValue(((Enum)enumItem).name()); 
       param.getOption().add(option); 
      } 
     } 
     return param; 
    } 

    @Override 
    public void setWadlGeneratorDelegate(WadlGenerator delegate) { 
     this._delegate = delegate; 
    } 

    @Override 
    public Application createApplication() { 
     return _delegate.createApplication(); 
    } 

    ... all other methods also simply call the _delegate equivalent method  
} 

當然,將其插入到你的鏈,做這樣的事情:

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") 
      .generator(OptionsWadlGenerator.class).descriptions(); 
    } 
} 
3

快速搜索com.sun.research.ws.wadl.Param.getOption()(請參見結果here)的用法顯示它實際上從未從庫中調用過。我想這只是因爲這些類是由wadl.xsd中的xjc生成的。似乎澤西基本上忽略了wadl文件中的這條信息,同樣不關心將它包含在它生成的wadl文件中。

幾年前,我們最終編寫了自己的代碼來生成wadl,因爲可用的工具非常差。這可能從那時起就發生了變化,但上述問題表明,對wadl的適當支持仍然不夠。 :(

+0

謝謝你的努力。我也沒有看到此功能的任何參考,但認爲它可能通過反射以某種方式暗中調用。 – AlexR 2013-02-21 17:55:44

+0

只有少數'WadlGenerator'實現者,'createParam()'在它們中都不是非常複雜,所以我只是得出結論,如果沒有對其getter的引用,選項列表將被忽略。 – zagyi 2013-02-21 18:08:14

相關問題