1

I'm attempting to use springfox 2.6.1 (springfox-swagger2 and springfox-swagger-ui) with a Spring MVC application to generate swagger docs. My application has many existing REST api's however I only want to swaggerize one of them at the moment. It resembles the following:

@RestController
public class MyController {

    @RequestMapping(value = "/getapi/{key}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "get something", response = MyEntity.class)
    @ApiResponse(code = 404, message = "Not found")
    public ResponseEntity<String> find(@ApiParam(value = "", required = true) @PathVariable String key) {
        ...
    }

I've followed a couple of examples to help me get started:

  1. http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
  2. A 'simple' way to implement Swagger in a Spring MVC application

After starting the application and navigating to http://host/my-app/swagger-ui.html I see the docs for my REST controller, however under the list of operations is every possible http method. That is a GET, POST, PUT, DELETE, etc for the same REST endpoint. I would expect to see only a GET operation. Below is my bean configuration. Is something missing or misconfigured?

@Configuration
@EnableSwagger2
public class Config{

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
          .select()             
          .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
          .build()
          .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("My API").version("v1").build();
    }

}
Community
  • 1
  • 1
user1491636
  • 2,355
  • 11
  • 44
  • 71
  • The reason all http methods are generated is because springfox is unable to detect the method from the RequestMapping annotation. Add an explicit method, in this case GET to the annotation, e.g.: @RequestMapping(value = "/getapi/{key}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET) – gary schulte Aug 25 '17 at 18:01

4 Answers4

3

SpringFox by default generates swagger for all the HTTP Verbs if one is not specified in your request mapping. In your controller's 'find' method, specify a request method. This will fix your problem.

Example:

@RequestMapping(value = "/getapi/{key}", 
method=RequestMethod.GET, 
produces = MediaType.APPLICATION_JSON_VALUE)
Tito
  • 8,894
  • 12
  • 52
  • 86
  • Yes, this worked for me. If we dont specify method attribute for RequestMapping, then SpringFox generates swagger for all http verbs. – Venky Sep 10 '18 at 14:39
1

I couldn't find the exact reason why this was happening, however I was able to resolve it by adding annotation io.swagger.annotations.Api to the rest controller, and additionally modifying the Docket to:

    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
            .build()
            .apiInfo(apiInfo());
user1491636
  • 2,355
  • 11
  • 44
  • 71
0

Annotate your method with @ApiIgnore which you don't want to expose and try once.

Vaibs
  • 1,546
  • 9
  • 31
0

This can be fixed by using method type in RequestMapping

@RequestMapping(method = RequestMethod.GET, value = "/associates/{associateId}")
Kumar Ajay
  • 222
  • 4
  • 10