为什么Swagger会在请求网址中包含不必要的变量?

时间:2015-12-04 07:42:54

标签: spring-mvc swagger swagger-ui

我目前正在记录我的REST界面" / news / rest / v1 / articles"使用Swagger2和Spring MVC。它看起来像这样:

@RestController
@Api(
        description = "My News interface",
        produces = "application/json; charset=utf-8"
)
public class NewsController {
    @RequestMapping(
            method = RequestMethod.GET,
            value = "/news/rest/v1/articles",
            produces = {"application/json; charset=utf-8"}
    )
    @ApiOperation(
            value = "Returns news according to the given limit and language"
    )
    @ApiResponses(
            value = {
                    @ApiResponse(
                            code = 503,
                            message = "in case the news cannot be retrieved"
                    )
            }
    )
    public String getNews(
            @ApiParam(
                    value = "limitation of news elements",
                    required = false,
                    defaultValue = "-1" // -1 means no limitation
            )
            @RequestParam(defaultValue = "-1")
            Integer limit,

            @ApiParam(
                    value = "the language",
                    required = true,
                    allowableValues = "de,fr,it,en"
            )
            @RequestParam(defaultValue = "de")
            @Valid
            @Pattern(regexp = "de|fr|it|en")
            String language

            throws HTTPException {
        // implementation
    }
}

如果我部署我的应用并导航到Swagger-UI,一切看起来都很好,除了我不能使用"试一试!" - 按钮。问题是Swagger使用的生成的URL有点奇怪......看起来像这样:

http://localhost:8080/myApp/api/news/rest/v1/articles{?limit,language}?limit=-1&language=de

我无法理解为什么字符串" {?limit,language}"包含在请求URL中。如果我手动调用那个没有" {?limit,language}"字符串,它工作得很好......

如果有人能给我一个指针,我会非常感激。

祝你好运 沃尔特

1 个答案:

答案 0 :(得分:2)

我在这里遇到了同样的问题:Using @RequestParam annotated method with swagger ui

您需要设置

enableUrlTemplating(false)
Docket配置

中的