2017-02-20 282 views
2

我在Spring Boot應用程序中使用Springfox Swagger2版本2.4.0,Springfox Swagger UI版本2.4.0和Swagger註解版本1.5.0。在Swagger UI中使用Springfox-Swagger2自定義請求頭描述

這裏的問題是,我能夠爲我的控制器的API生成swagger UI,並且我能夠測試相同。但是我無法爲我的請求標題指定請求標題描述。我使用@RequestHeader註解相同。

在我的控制器API的代碼片段是如下:

@RequestHeader(name = "Api-Key") String apiKey

的揚鞭UI用於請求頭是如下:

enter image description here

在圖像中突出顯示的矩形區域代表請求頭的描述。

目前它只是拿起name屬性中提到的數據並顯示它。但我想給出不同的描述。即「許可證密鑰的價值」

如何在Swagger UI中實現此目標,因爲@RequestHeader註釋只具有value,defaultValue,name和required屬性?任何幫助將非常感激。

更新:尋找一個解決方案開箱沒有我自己的

+0

不明白爲什麼這個問題是downvoted兩次? – Gandhi

回答

3

TL的任何自定義註釋; DR是,你將不得不建立自己的插件來做到這一點。

基本上,在這種情況下,爲了增加描述的唯一開箱即用的註釋是@ApiParam並且更準確@ApiImplicitParam。不幸的是,這些註釋都不支持描述。

所以我的建議是:

  1. 創建您自己的註釋,看起來像這樣

    @RequestHeader(name = "Api-Key") @Description("Value of license key") String apiKey

注:已經有一個annotation in spring適於這個。

  • 創建自己ParameterBuilderPlugin
  • 實現插件如下所示
  • public class Test implements ParameterBuilderPlugin { 
        @Override 
        public void apply(ParameterContext parameterContext) { 
        ResolvedMethodParameter methodParameter =parameterContext.resolvedMethodParameter(); 
        Optional<Description> requestParam = methodParameter.findAnnotation(Description.class); 
        if (requestParam.isPresent()) { 
         parameterContext.parameterBuilder() 
         .description(requestParam.get().value()); 
        } 
        } 
    
        @Override 
        public boolean supports(DocumentationType documentationType) { 
        return false; 
        } 
    } 
    
  • 挑選的the order一個值,該值已應用after swagger annotations已處理。
  • 另外,請將您的springfox庫升級到latest version

    +0

    感謝您的快速回復。不要對你說的事情有太多的曝光,所以會嘗試並回復你。再次感謝 – Gandhi

    +0

    終於有時間開始了。但我堅持一開始,因爲我相當新的這一點。正如你所提到的,我嘗試將描述註釋放在我的請求頭部參數中,並得到一個錯誤 - 「註釋類型不適用於這種聲明」您能指導我嗎?我也應該在我的春季應用程序代碼中將測試類放在您的文章中?最後,我不是很清楚你在第4步中的含義請諮詢 – Gandhi

    +0

    對此有何建議? – Gandhi

    1

    也許我的回答會幫助別人。

    Dilip Krishnanhis answer中,您可以使用io.swagger.annotations.ApiParamio.swagger.annotations.ApiImplicitParam Swagger批註用於精細調整的自定義文檔。

    @ApiParam可以用於註冊方法參數。

    @ApiImplicitParam可以在API參數沒有明確註冊的情況下使用。

    @RestController 
    @RequestMapping(value = "/v1/test", produces = "application/json") 
    @Api(value = "/v1/test") 
    public class TestController { 
    
        @ApiOperation(value = "Do Something method", tags = "Test method") 
        @RequestMapping(value = "/doSomeThing", method = RequestMethod.GET) 
        public Foo doSomeThing(
          @ApiParam(value = "Param1 Description", required = true) 
          @RequestParam String param) { 
         throw new UnsupportedOperationException("do Some Things"); 
        } 
    
        @ApiOperation(value = "Do Something Another method", tags = "Test method") 
        @ApiImplicitParams({ 
          @ApiImplicitParam(name = "anotherParam1", value = "Another Param1 Description", paramType = "header"), 
          @ApiImplicitParam(name = "anotherParam1", value = "Another Param1 Description", paramType = "header") 
        }) 
        @RequestMapping(value = "/doSomeThingAnother", method = RequestMethod.GET) 
        public Foo doSomeThingAnother(Bar bar) { 
         throw new UnsupportedOperationException("do Some Thing Another"); 
        } 
    
    
    }  
    

    而在最後,你可以看到下面的圖片

    Swagger UI for custom method description

    +0

    我正在尋找一些要做的事情在請求頭參數。不幸的是不是隱含的參數 – Gandhi

    +0

    描述已更新爲常規請求參數。 –

    +0

    '@ApiParam(value =「header description」)'可以和'@ RequestHeader'一起使用來提供標題描述。 @DmytroBoichenko用這個更新答案。 –