2
votes

Swagger configured in Spring Boot shows only one method with POST mapping and one method with GET mapping from every controller. Swagger ignores another methods with GET and POST mapping and ignores all methods with PUT and DELETE mappings. My configuration:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("my.project.controllers"))
                .paths(PathSelectors.ant("/api/*"))
                .build();
    }
}

Dependency in pom.xml:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
            <scope>compile</scope>
        </dependency>

My controllers code:

@RestController @RequestMapping(value = "/api/users", produces = "application/json; charset=UTF-8") public class UserController {

@Autowired
private UserService userService;

protected UserService getService() {
    return userService;
}

@RequestMapping(method = GET)
public Page<User> query(@RequestParam Map<String, Object> parameters, Pageable pageable) {
    return getService().query(parameters, pageable);
}

@ResponseStatus(CREATED)
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<User> create(@RequestBody User entity) {
    return ResponseEntity.status(HttpStatus.CREATED).body(getService().create(entity));
}

@RequestMapping(value = "/{id:[0-9]+}", method = RequestMethod.PUT)
public ResponseEntity<User> update(@PathVariable Long id, @RequestBody User entity) {
    return ResponseEntity.ok(getService().update(id, entity));
}

@RequestMapping("/current")
public ResponseEntity current() {
    return ResponseEntity.ok(userService.getUser());
}

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/{id:[0-9]+}/enable", method = RequestMethod.POST)
public void enable(@PathVariable("id") final long id) {
    userService.enable(id);
}

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/{id:[0-9]+}/disable", method = RequestMethod.POST)
public void disable(@PathVariable("id") final long id) {
    userService.disable(id);
}

@RequestMapping(value = "/histories", method = RequestMethod.GET)
public List<UserHistory> histories() {
    return userService.histories();
}

}

May be i need add some more configuration or add something else?

1
can you please show some controller code?zakaria amine
I added controller code. Other controller are the same. Swagger shows only query method and create method.Artiom Saidanov

1 Answers

3
votes

Based on your controller, I think you should add one more star in the path matcher in your swagger config:

.paths(PathSelectors.ant("/api/**"))

e.g /api/users/current would not be matched by the /api/* but by /api/**, and this why you are getting only the base path endpoints documented.