Controller does not appear in swagger-ui.html
Asked Answered
H

3

8

I use Swagger 2 with non-spring-boot and I also can deploy my app on Tomcat and run it successfully. But the controller does not appear in swagger-ui.html, just the empty page with the green swagger title show up. I have spent two days on this issue. Would you give me some advice?

@Controller means the class as bellow:

 @Api
 @Controller
 @RequestMapping("/user")
 public class UserController {

 protected Logger logger = LoggerFactory.getLogger(UserController.class);

 @Autowired
 private UserService userService;

 @RequestMapping("/showInfos")
 public @ResponseBody Object showUserInfos(){
    logger.info("-----------------------showUserInfos-----------------------");
    List<UserInfo> userInfos = userService.getUsers();
    return userInfos;
}

my spring-mvc.xml configuration as follows:

    <mvc:annotation-driven/>
<!@Controller inject bean -->
<context:component-scan base-package="com.roy.demo , version" /> 

<!-- Enables swgger ui -->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" /> 

<!-- Include a swagger configuration -->
<bean name="/applicationSwaggerConfig" class="com.roy.demo.config.ApplicationSwaggerConfig" />

also my swagger configuration class is as follows:

@EnableSwagger2  
public class ApplicationSwaggerConfig {

private static final Logger LOGGER = Logger.getLogger(ApplicationSwaggerConfig.class);

@Bean
public Docket api() {
    LOGGER.info("################################ into Docket api() #####################################");
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.roy.demo.controller"))
            .paths(PathSelectors.any())
            .build();
}

}

my maven pom.xml swagger2 dependency as follows:

        <!-- Swagger 2.0 -->
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-core</artifactId>
        <version>1.5.3</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.5.0</version>
    </dependency>

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

Bellow is the result when i enter the endpoint url:http://localhost:8080/Spring_SpringMVC_Mybatis/swagger-ui.html

enter image description here

Hyperextension answered 21/9, 2016 at 3:28 Comment(3)
what is mean by @Controller can not show in swagger-ui.Stonwin
share your config class for swaggerStonwin
Please follow this answer for same :- https://mcmap.net/q/1468780/-swagger-2-0-for-spring-mvc-not-workingCapparidaceous
S
4

I also new to Swagger but Below code I used for my swagger configuration and it works well for me.I done the configuration in class.

Configuration class.

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = "com.*")
@PropertySource(value = { "classpath:log4j.properties" })
public class SpringConfig extends WebMvcConfigurerAdapter {
    @Bean
            public Docket api() { 
                return new Docket(DocumentationType.SWAGGER_2)  .apiInfo(apiInfo()).directModelSubstitute(LocalDate.class, String.class).genericModelSubstitutes(ResponseEntity.class)
                        .useDefaultResponseMessages(false)
                  .select()           
                  .apis(RequestHandlerSelectors.any())              
                  .paths(PathSelectors.any())                        
                  .build();                                           
            }
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("swagger-ui.html")
                  .addResourceLocations("classpath:/META-INF/resources/");
                registry.addResourceHandler("/webjars/**")
                  .addResourceLocations("classpath:/META-INF/resources/webjars/");
            }
    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
          "API",
          "API for xxxx",
          "API TOS",
          "Terms of service",
          "xxx",
          "License of API",
          "");
        return apiInfo;
    }

}

Maven Dependency:

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

Controller Class

@RestController
@Api(value="users", description="Endpoint for user management")
public class Controller {
}

endpointurl:

https://localhost:8080/AppName/swagger-ui.html
Stonwin answered 22/9, 2016 at 6:38 Comment(4)
i.sstatic.net/l3wag.png I still can't solve this issue, see the picture i cut aboveHyperextension
have you changed the configuration?Stonwin
Yes, I did as all you did to the swagger configuration class,but still can not workHyperextension
Took your code but its not working on my machine might be missing some depedencies.Remove swagger-core dependency from pom also put !Api annotation at class level and remove @ApiOperation(value = "获取用户列表", notes = "") from method and changed the .apis from swaggerconfig class to RequestHandlerSelectors.any() and try onceStonwin
T
3

clear your browser cache and try again. or use incognito tab It worked well for me

Tupler answered 22/1, 2021 at 5:27 Comment(0)
P
1

It happened to me that one of the controllers was not displayed. The problem was the kotlin class has not declared any package. Declaring a package in the controller fixed the problem.

Prescribe answered 26/1, 2021 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.