Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller does not appear in swagger-ui.html

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

like image 733
Roy Wu Avatar asked Sep 21 '16 03:09

Roy Wu


People also ask

Why is Swagger UI not showing the JSON Doc?

The json is being generated correctly so Swagger UI should display the doc as well, but this is not happening, the UI is shown as well but with no documentation in it. They're all in the same package.

Why are my controllers not showing up in the swagger plugin?

The problem was the package. The plugin also auto generates the SwaggerDocumentationConfig class which includes all controllers from the API package that is configured. My own controllers had to be in the exact same package. The auto generated SwaggerDocumentationConfig class contains:

Where can I find the Swagger UI?

The Swagger UI can be found at https://localhost:<port>/swagger: Now we can explore the API via the Swagger UI and it will be easier to incorporate it into other applications. We can see each controller and its action methods listed here.

What is Swagger in ASP NET Core?

Swagger UI offers a web-based UI that provides information about the service. This is built using the Swagger Specification and embedded inside the Swashbuckle package and hence it can be hosted in our ASP.NET Core app using middlewares. Integrating Swagger UI into our Applications


1 Answers

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
like image 124
Vaibs Avatar answered Sep 28 '22 02:09

Vaibs