2

我正在将 Springfox 2.2.2 集成到我的 Spring MVC 项目中,但没有像我想的那样生成 api-docs。下面是一些关于我的配置的信息。

我提供了以下依赖项(连同其他库,如 fastxml、webjar、使用正确版本的 spring 等)

    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.2.2</version>
</dependency>

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

Springfox配置如下:

    package com.exemplarypackage.config;
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.exemplarypackage.controller")
public class SwaggerConfig extends WebMvcConfigurerAdapter{
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                "My Project's REST API", 
                "This is a description of your API.", 
                "API TOS",
                "url",
                "me@wherever.com", 
                "API License", 
                "API License URL");
        return apiInfo;
    }

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

    }

示例性控制器如下所示:

package com.exemplarypackage.controller;
@Api(value = "test class for springfox")
@Controller
public class TestController {
    @ApiOperation(value = "Returns test details")
    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "Successful retrieval", response = Test.class),
        @ApiResponse(code = 404, message = "Test does not exist"),
        @ApiResponse(code = 500, message = "Internal server error")}
    )
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(Locale locale, Model model) {
        logger.info("TEST");
        return "test";
    }
}

使用上述设置,当我执行 url: localserver:8080/myApp/swagger-ui 时,几乎没有任何内容可显示,但没有错误消息。

然后,我将在 spring-fox-swagger-ui-2.2.2.jar 中找到的内容添加到 src/main/resources/META-INF(我将其解压缩并粘贴到给定文件夹)。现在,当我转到 localserver:8080/myApp/swagger-ui 时,所有绿色图形都会显示,但没有 api 文档。我注意到在服务器日志中 swagger-ui 会查找 swagger-resources 端点,但它会得到 404。当我浏览服务器日志时,我看到没有创建这样的端点:swagger-resources、v2/api-docs 等。但是,我注意到这些类被过滤为 swagger 注释......有一个 springfox。 META-INF/resources/webjars/springfox-swagger-ui 文件夹中包含 swagger-resorces 端点的 Node.js 文件 - 也许它应该切换到不同的名称?

我不知道如何让它工作......我应该以某种方式声明这些端点还是应该自动创建它们?也许我只是错过了一些小东西,但我最近几天一直在与这个问题作斗争,无法弄清楚应该配置什么才能使其正常工作。

4

2 回答 2

3

在@zubactick 的帮助下,我设法解决了这个问题。目前,我对 springfox 和 mvc 有单独的配置类。所以,我的 springfox 配置是这样的:

package com.myPackage.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig{


    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .groupName("test")
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                "My Project's REST API", 
                "This is a description of your API.", 
                "API TOS",
                "url",
                "me@wherever.com", 
                "API License", 
                "API License URL");
        return apiInfo;
    }

}

我的 MVC 配置是这样的:

package com.myPackage.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@Import({SwaggerConfig.class})
@ComponentScan("com.myPackage.controller")
public class WebSpringConfig extends WebMvcConfigurerAdapter{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        registry
            .addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
        registry
            .addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public ViewResolver configureViewResolver() {
        InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
        viewResolve.setPrefix("/WEB-INF/views/");
        viewResolve.setSuffix(".jsp");

        return viewResolve;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}   

此外,在 web.xml 文件中指定了以下内容:

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.myPackage.config.WebSpringConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

控制器可以与问题中提供的相同,但是使用上面的 swagger 配置,我声明的包中的所有控制器都由 swagger 扫描和记录。

于 2015-12-02T10:48:12.240 回答
0

@gromajus,您能否尝试在示例控制器类中使用@RestController注释而不是注释?@Controller

于 2015-11-20T13:01:46.913 回答