简体   繁体   中英

Swagger configured in Spring Boot shows only methods with POST and GET mapping

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?

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

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

eg /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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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