简体   繁体   English

Springfox Swagger2-@ApiOperation不起作用

[英]Springfox Swagger2 - @ApiOperation not working

I use springfox-swagger2 for my Spring MVC REST API. 我为Spring MVC REST API使用springfox-swagger2。 Everything works good with swagger but my problem is I cannot add additional information to my swagger documentation. swagger一切正常,但是我的问题是我无法在swagger文档中添加其他信息。

Maven Dependency: 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 config class: 我的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;
    }
}

My controller class: 我的控制器类:

@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
    }
}

So, calling the swagger-ui the controller is shown, the method, everything except my additional infos defined in @ApiOperation and @ApiImplicitParams . 因此,显示了调用swagger-ui控制器,方法,除@ApiOperation@ApiImplicitParams定义的其他信息以外的所有内容。 Does anyone have an idea from where the problem can come from? 有谁知道问题可能来自何处? The params are also not in the JSON file which is created from swagger. 这些参数也不在使用swagger创建的JSON文件中。

Try to replace your customImplementation() method by: 尝试通过以下方式替换您的customImplementation()方法:

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

Build the project, and then your additional infos should appear. 生成项目,然后您的其他信息应出现。

EDIT : I don't know if it makes any difference, but I am using these dependencies: 编辑 :我不知道它是否有任何区别,但我正在使用这些依赖项:

    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM