2014-11-14 59 views
5

在我的項目中,我們使用RestEasy創建REST接口並使用Swagger來記錄它們。問題是,這需要許多註釋,並可能看起來像下面這樣:Messy REST註釋

@ApiOperation(value = "Create a person object", 
     notes = "Create a person object" + 
       "Return the newley created person object", 
     response = Person.class) 
@ApiResponses({ 
     @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "Internal server error"), 
     @ApiResponse(code = HttpStatus.SC_UNAUTHORIZED, message = "Unauthorized"), 
     @ApiResponse(code = HttpStatus.SC_PRECONDITION_FAILED, message = "Precondition failed"), 
     @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "Bad request"), 
     @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY, message = "Unprocessable entity") 
}) 
@POST 
@Path("rest/v1/persons") 
@Consumes({MediaType.APPLICATION_JSON}) 
@Produces({MediaType.APPLICATION_JSON}) 
Person createPerson(
     @HeaderParam("SecurityToken") String token, 
     @ApiParam(value = "person", defaultValue = "{ \"name\": = \"Bart Simpson\", \"age\": = 9 }") Person person); 

大多數註釋看起來或多或少在我們所有的方法都是一致的。所以我們複製並粘貼了很多內容,所有這些註釋都使得我們的界面變得不可讀,並且很難準確判斷這些方法在做什麼。

所以我想知道如果任何人有一個想法,我們如何可以有相同的功能,但以某種方式隱藏所有這些註釋,或至少其中一些。

+0

我不知道Swagger,但也許它支持刻板印象。這樣你可能能夠將所有這些@ApiXxx標註減少到一個。 – Thomas 2014-11-14 15:53:04

+0

我假設主要問題是與@ApiResponses? – Ron 2014-11-14 15:54:22

+0

Swagger是否支持元註釋?這是通常的Spring方法。 – chrylis 2014-11-14 16:43:10

回答

0

創建自定義註釋並使用jaxrs和swagger註釋對其進行註釋。 Spring的RestController就是一個很好的例子:

@Controller 
@ResponseBody 
public @interface RestController { 

} 

編輯:如果元註釋不揚鞭和/或RestEasy的支持,您可以嘗試APT作爲解決方案。