首页 > 解决方案 > 由于 PropertySourcedMapping,覆盖 swagger 2 api-docs 不会导致我的控制器被调用

问题描述

我试图用我自己的控制器覆盖 api-docs 端点,这似乎不起作用。

我已在 application.properties 中设置

springfox.documentation.swagger.v2.path=/somethingelse

然后我制作了自己的控制器

@RequestMapping(
    value = "/v2/api-docs",
    method = RequestMethod.GET,
    produces = { APPLICATION_JSON_VALUE, "application/hal+json" })

这是试图覆盖由Swagger2Controllerwhich 给出的响应,如下所示:

  @RequestMapping(
      value = DEFAULT_URL, // /v2/api-docs
      method = RequestMethod.GET,
      produces = { APPLICATION_JSON_VALUE, HAL_MEDIA_TYPE })
  @PropertySourcedMapping(
      value = "${springfox.documentation.swagger.v2.path}",
      propertyKey = "springfox.documentation.swagger.v2.path")
  @ResponseBody
  public ResponseEntity<Json> getDocumentation(
      @RequestParam(value = "group", required = false) String swaggerGroup,
      HttpServletRequest servletRequest) {

由于某种原因,请求仍将发送到该控制器而不是我的控制器。

奇怪的是,如果我的控制器不存在请求/v2/api-docs不会响应。

春狐:2.9.2 春:5.1.4

标签: javaspringspring-mvcspringfox

解决方案


A 类扩展Swagger2DocumentationConfiguration

覆盖

public HandlerMapping swagger2ControllerMapping(Environment environment, DocumentationCache documentationCache,
  ServiceModelToSwagger2Mapper mapper, JsonSerializer jsonSerializer) {
    return new PropertySourcedRequestMappingHandlerMapping(
        environment,
        new CustomSwagger2Controller(environment, documentationCache, mapper, jsonSerializer));
}

CustomSwagger2Controller是我的自定义控制器


推荐阅读