控制器没有出现在swagger-ui.html中

时间:2016-09-21 03:28:25

标签: spring-mvc swagger-2.0

我将Swagger 2与非spring-boot一起使用,我也可以在Tomcat上部署我的应用程序并成功运行它。但是控制器没有显示在swagger-ui.html中,只显示带有绿色招摇标题的空白页面。我在这个问题上花了两天时间。你能给我一些建议吗?

@Controller意味着类如下:

 @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;
}

我的spring-mvc.xml配置如下:

    <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" />

我的招摇配置类如下:

@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();
}

}

我的maven pom.xml swagger2依赖关系如下:

        <!-- 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是我输入端点网址时的结果:http://localhost:8080/Spring_SpringMVC_Mybatis/swagger-ui.html

enter image description here

3 个答案:

答案 0 :(得分:1)

我也是Swagger的新手,但是下面的代码用于我的swagger配置,它适用于我。我在课堂上完成了配置。

配置类。

@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>
            <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>

控制器类

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

endpointurl:

https://localhost:8080/AppName/swagger-ui.html

答案 1 :(得分:0)

清除浏览器缓存并重试。 或使用隐身标签 对我来说效果很好

答案 2 :(得分:0)

我碰巧没有显示其中一个控制器。问题是 kotlin 类没有声明任何包。在控制器中声明一个包解决了这个问题。