Spring Boot 中配置的 Swagger 仅显示具有 POST 和 GET 映射的方法

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

在 Spring 引导中配置的 Swagger 只显示了一种使用 POST 映射的方法和一种使用 GET 的方法来自每个控制器的映射。 Swagger 忽略另一种使用 GETPOST 映射的方法,并忽略所有使用 PUT删除 映射。我的配置:

@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();
    }
}

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>

我的控制器代码:

@RestController @RequestMapping(value = "/api/users", produces = "application/json; charset=UTF-8") public class 用户控制器 {

@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();
}

}

可能我需要添加更多配置或添加其他内容?

根据您的控制器,我认为您应该在 swagger 配置中的路径匹配器中再添加一颗星:

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

例如 /api/users/current 不会与 /api/* 匹配,而是与 /api/** 匹配,这就是为什么您只记录了基本路径端点的原因。