Springfox Swagger2 - @ApiOperation无效

时间:2016-02-19 11:21:58

标签: java swagger-ui swagger-2.0 springfox

我使用springfox-swagger2作为我的Spring MVC REST API。一切都很好用,但我的问题是我无法在我的招摇文档中添加其他信息。

Maven依赖:

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.2.2</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.2.2</version>
</dependency>

My Swagger配置类:

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan("path.to.controller.package")
public class SwaggerConfig {

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SPRING_WEB).apiInfo(apiInfo());
    }

    @Bean
    public UiConfiguration uiConfig() {

        return UiConfiguration.DEFAULT;
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo("Service API", "Simple REST Service", "0.0.1",
                "mail@mail.com", "mail@mail.com", " ", " ");
        return apiInfo;
    }
}

我的控制器类:

@RestController
@RequestMapping("/persons")
public class PersonController {

    Logger LOGGER = LoggerFactory.getLogger(PersonController.class);

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
    @ApiOperation(value = "doStuff", response = Person.class)
    @ApiImplicitParams({@ApiImplicitParam(name="Authorization", value="MY DESCRIPTION")})
    public @ResponseBody Person getPerson(@PathVariable String id,
          @RequestHeader(value = "Authorization") String authToken) throws Exception {

          //do things and return
    }
}

因此,显示调用控制器的swagger-ui,方法,除@ApiOperation@ApiImplicitParams中定义的其他信息之外的所有内容。有没有人知道问题出在哪里? params也不在由swagger创建的JSON文件中。

1 个答案:

答案 0 :(得分:2)

尝试将customImplementation()方法替换为:

@Bean
public Docket customImplementation() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .build()
        .apiInfo(apiInfo());
}    

构建项目,然后应显示您的其他信息。

编辑:我不知道它是否有任何区别,但我正在使用这些依赖项:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.3.1</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.1.2</version>
    </dependency>