Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 为什么Swagger会在请求URL中包含不必要的变量?_Spring Mvc_Swagger_Swagger Ui - Fatal编程技术网

Spring mvc 为什么Swagger会在请求URL中包含不必要的变量?

Spring mvc 为什么Swagger会在请求URL中包含不必要的变量?,spring-mvc,swagger,swagger-ui,Spring Mvc,Swagger,Swagger Ui,我目前正在接受培训,使用Swagger2和SpringMVC记录我的REST界面“/news/REST/v1/articles”。看起来是这样的: @RestController @Api( description = "My News interface", produces = "application/json; charset=utf-8" ) public class NewsController { @RequestMapping(

我目前正在接受培训,使用Swagger2和SpringMVC记录我的REST界面“/news/REST/v1/articles”。看起来是这样的:

@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
    }
}
http://localhost:8080/myApp/api/news/rest/v1/articles{?limit,language}?limit=-1&language=de
如果我部署我的应用程序并导航到招摇过市的用户界面,一切看起来都很好,除了我不能使用“试用”按钮。问题是,Swagger使用的生成URL有点奇怪……它看起来像这样:

@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
    }
}
http://localhost:8080/myApp/api/news/rest/v1/articles{?limit,language}?limit=-1&language=de
我无法完全理解为什么请求URL中包含字符串“{?limit,language}”。如果我在没有“{?limit,language}”字符串的情况下手动调用该URL,它就可以正常工作

如果有人能在这方面给我指点,我将不胜感激

致意
沃尔特

我在这里遇到了同样的问题:

你需要设置

enableUrlTemplating(false)

Docket
configuration

中,因为您的方法需要它们。这是什么意思?当然,这个方法需要这些参数……那你为什么要问?生成的URL只是告诉您这一点
{}
是一个模板占位符,以防您不知道。当点击“试用!”-按钮时,Swagger调用“{?limit,language}?limit=-1&language=de”,而不是”“。为什么字符串“{?limit,language}”包含在第一位?可能是