Search code examples
spring-bootswaggerswagger-uispringfox

swagger UI doesn't show apis while all configuration seems to be right


I have a springboot helloword project, the structure isstructure of project

and my swaggerConfig is


    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //.enable(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("org/jayden/swaggertest/controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot and Swagger2")
                .description("desc")
                .termsOfServiceUrl("http://localhost:8080/")
                .version("1.0.0")
                .build();
    }
    }

My controller is:

    @RestController
    @RequestMapping("/user")
    @Api(value = "testApi", tags = "test")
    public class UserController {


    /**
     * @param
     * @return
     */
    @PostMapping(value = "/save")
    @ApiImplicitParam(name = "user", value = "addUser")
    @ApiOperation(value = "addUser", notes = "addUserByParm")
    public String saveUser(){
        return "added";
    }

I use porm.xml:


            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

Could you please tell me what mistake I've made. I've got the wrong message from swagger-ui: wrong message from swagger-ui

I've got wrong message from console in chrome: wrong in chrome console This is my first time to ask question on stackOverflow, sorry for any unclear descriptions. Thank you guys!


Solution

  • You can try

    RequestHandlerSelectors.basePackage("org/jayden/swaggertest/controller")
    

    instead of

    RequestHandlerSelectors.basePackage("org.jayden.swaggertest.controller")