2016-05-05 9 views
0

我使用Swagger2與Springfox和Spring Boot。我有像這樣限定的端點:ID不應該顯示模型架構與Swagger + Spring

@ApiOperation(value = "save", nickname = "Save Store") 
@ApiResponses(value = { 
     @ApiResponse(code = 201, message = "Created"), 
     @ApiResponse(code = 401, message = "Unauthorized"), 
     @ApiResponse(code = 403, message = "Forbidden"), 
     @ApiResponse(code = 500, message = "Failure", response = ErrorResource.class)}) 
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 
@ResponseStatus(HttpStatus.CREATED) 
public void save(@Valid @RequestBody Store store, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) { 
    if (bindingResult.hasErrors()) { 
     throw new InvalidRequestException("Invalid Store", bindingResult); 
    } 

    this.storeService.save(store); 
    response.setHeader("Location", request.getRequestURL().append("/").append(store.getId()).toString()); 
} 

所生成的API文檔中示出了在模型模式的Storeid。從技術上講,在創建Store時,JSON不應包含id。我試圖弄清楚如何告訴Swagger/Springfox忽略id,但僅限於此端點。

回答

0

您可以通過@ApiModelProperty註釋類的屬性及其隱藏屬性設置爲true隱藏模型的領域。

import io.swagger.annotations.ApiModelProperty; 

public class Store { 

    @ApiModelProperty(hidden = true) 
    private Long id; 

} 

不幸的是,通過這樣做,您將隱藏使用Store類作爲輸入的每個端點上的id字段。顯示另一個端點的字段將需要一個單獨的類。

+0

是的,那不是我所需要的。我昨天使用'@ JsonView'找出了它。我會發布我自己的答案。 (還是)感謝你的建議。 – Gregg

+0

你對@Gregg做了什麼? JsonView似乎不起作用。 – NeilS