修改SwaggerUI以删除所需的PathParam

时间:2016-01-14 04:14:49

标签: spring-boot swagger swagger-ui swagger-2.0

我需要使用可选路径参数。所以就像下面这样;

@ApiOperation(httpMethod = "GET", value = "Get User Details With Optional Path Parameters", notes = "Output depends on values provided")
@ApiResponses(value = {
        @ApiResponse(code = 404, message = "We do not unserstand what you mean"),
        @ApiResponse(code = 400, message = "You are not requesting like a BOSS.") })
@RequestMapping(value = { "/getuser/userid/{userid}",
        "/getuser",
        "/getuser/userid/{userid}/alias/{alias}", "getuser/alias/{alias}" }, method = RequestMethod.GET, produces = {
        MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseStatus(HttpStatus.OK)
private UserSchema getUserDetails(
        @PathVariable Optional<String> userid,
        @PathVariable Optional<String> alias) {
    Users user = null;
    UserSchema returningSchema = buildDefaultSchema();
    if (alias.isPresent()) {
        //Get The Value
    } else {
        //Try Get Other Value and do stuff etc.
    }
    //Similar for userid

    try {
        //Get User Data From DB
        user = dao.getUserData(userid,alias);
        //Bind data to returning schema
    } catch (Exception ex) {
        Log.error(getClass().getName(), ex);
        returningSchema.setResponseText("Something is Wrong");
    } 
    return returningSchema;
}

但由于招摇,它不允许发出请求,因为PathVariablesrequired类型。我不太了解javascript。尝试this解决方案来修改swagger-ui.js,但似乎迷失在巨大的文件中,找不到提到的部分。

我使用最新的Swagger-UI版本。我是否有可能使用可选的路径变量发出请求,并且应该在swagger中显示正确的路径?

注意:我知道swagger规范不允许使用可选路径变量。但是我想在我的应用程序中更改它。

感谢。

1 个答案:

答案 0 :(得分:0)

是的,你绝对可以用swagger-js和swagger-u来支持这个。请查看Operation.prototype.getMissingParams中的operation.js