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

时间:2017-08-04 14:44:10

标签: spring spring-boot configuration swagger swagger-ui

在Spring Boot中配置的Swagger仅显示一个带有 POST 映射的方法,以及一个带有来自每个控制器的 GET 映射的方法。 Swagger忽略了使用 GET POST 映射的其他方法,并忽略了使用 PUT DELETE 映射的所有方法。我的配置:

@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 =&#34; / api / users&#34;,produce =&#34; application / json; charset = UTF-8&#34;) 公共类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();
}

}

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

1 个答案:

答案 0 :(得分:2)

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

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

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