2017-03-07 16 views
0

我正在使用Springfox和Swagger生成swagger文件。現在我正在使用@ModelAttribute從對象中提取變量(NetworkCmd)以在swagger文檔中顯示爲查詢參數。使用@ModelAttribute時未顯示自定義域對象

目前,我有以下控制器:

@RequestMapping(value = "/{product_id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
@ResponseHeader() 
public ResponseEntity<?> networkResponse(
    @RequestHeader HttpHeaders headers, 
    @PathVariable("product_id")String productId, 
    @Valid @ModelAttribute NetworkCmd cmd, 
    BindingResult result) 
    throws Exception { 

    ... 
} 

這裏是NetworkCmd樣本:

@ItemId 
@NotNull(message = "product cannot be null") 
@ApiModelProperty(
    value = "testing") 
private String product_id; 

@ApiModelProperty(
    value = "key", 
private String key; 

@ApiModelProperty(
    value = "parent") 
private Boolean is_parent_id; 

@Min(0) 
@ApiModelProperty(
    value = "radius") 
private double radius = 10d; 

一個在這個類的變量是一個自定義的域對象Nearby

private Nearby nearby = null; 

public Nearby getNearby() { 
    return nearby; 
} 

public void setNearby(String nearby) throws ParseException { 
    this.nearby = Nearby.parse(nearby); 
} 

這是怎樣的一個特殊變量,因爲它接受一個字符串,然後解析這個字符串,並把它變成了Nearby對象。

我的問題是,這個Nearby變量沒有顯示在生成的swagger文檔中,通過@ModelAttribute。我很樂意提供更多信息。

回答

1

解決此問題的一種方法是在您的文檔中創建備用類型規則。這種方式在任何時候我們遇到附近的類型,我們把它當作一個字符串。

new Docket(...) 
    .directModelSubstitute(Nearby.class, String.class) 
+0

感謝您的回答!不幸的是,這不起作用。我也試過: '.directModelSubstitute(Nearby.class,Object.class)' – ahatzz11

相關問題