【问题标题】:Why Swagger not showing all methods in same class?为什么 Swagger 不显示同一类中的所有方法?
【发布时间】:2019-04-05 15:32:58
【问题描述】:

我正在尝试在我的代码中使用 swagger,但并非所有方法都在 swagger-ui 中列出,有些方法没有显示

我使用的是 swagger 2.5.0 版本和 spring boot 2.1.0.RELEASE

我的用户休息控制器

@RestController
@RequestMapping(value = "/rest")
public class UserRestController {

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET, value = "/users")
    public Iterator<User> getUsers() {
        return userService.getUsers();
    }

    @RequestMapping(method = RequestMethod.GET, value = "/user/{id}")
    public User getUser(@PathVariable("id") Long id) {
        return userService.getUser(id);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/user")
    public User save(@RequestBody User user) {
        User userValidation = userService.getUser(user.getId());
        if (userValidation != null) {
            throw new IllegalAddException("username already used !");
        }
        return userService.save(user);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/user")
    public User delete(@RequestBody User user) {
        return userService.save(user);
    }

}

这是我的配置代码

@Configuration
@EnableSwagger2
public class SwaggerApi {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.social.core.rest")).paths(PathSelectors.ant("/rest/*"))
                .build().apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfo("Social API", "Soccial api for gamerz zone.", "API TOS", "Terms of service",
                new Contact("yali", "www.social.com", "prg@gmail.com"), "License of API",
                "API license URL");
    }
}

getUser 方法未在 swagger ui 中显示,当我点击 url 并已经获取数据时该方法有效

只显示了三种方法

【问题讨论】:

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


    【解决方案1】:

    我通过在路径中添加更多星号解决了这个问题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 2016-07-04
      • 2023-02-22
      • 1970-01-01
      相关资源
      最近更新 更多