Swagger和Spring MVC(非Spring- Boot )

y53ybaqx  于 6个月前  发布在  Spring
关注(0)|答案(2)|浏览(112)

我一直在尝试将Springfox-Swagger UI 3.0.0添加到一个带有Spring Security(非spring- Boot )的常规Spring MVC应用程序中,但无法弄清楚。我最近将swagger-ui合并到2个spring- Boot 项目中,这要容易得多,没有任何问题。
我已经遵循了许多教程,没有一个是工作。
我遵循的最新教程是SpringFox的官方教程,我还遵循了https://github.com/springfox/springfox-demos的演示项目,更具体地说,是spring-xml-swagger demo
请求/swagger-ui/、/swagger-ui/index.html、/swagger-ui.html和/v2/api-docs/ return 404。
我也试过旧版本的swagger-ui,但都不管用。因为这是公司项目,我不能透露太多,但任何建议都是好的。如果有必要,我会尝试用一些配置代码更新这篇文章。
来自pom.xml的文档

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

字符串
Spring Framework 5.2.x
我的SwaggerConfig.class。

@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
        @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
        private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Api Services")
                .description("Api Services")
                .version("v1")
                .build();
    }
    
    @Bean
    public UiConfiguration uiConfiguration() {
        return UiConfigurationBuilder
                .builder()
                .defaultModelsExpandDepth(-1)
                .build();
    }
}


在xml config中。

<mvc:resources mapping="/swagger-ui/**" location="classpath:/META-INF/resources/webjars/springfox-swagger-ui/"/>
    <mvc:view-controller path="/swagger-ui/" view-name="forward:/swagger-ui/index.html"/>
    <mvc:resources mapping="index.html" location="classpath:/META-INF/resources/webjars/springfox-swagger-ui/"/>
    <bean id="swaggerConfig" class="com.example.config.SwaggerConfig"/>

vhmi4jdf

vhmi4jdf1#

好吧,我不是很确定,但是in my experience at least some aspects of Spring MVC seem to be unhappy with a mix of XML and Annotation-based configuration,特别是@EnableWebMvc
在任何情况下,我们的工作应用程序,使用Spring 5.3.x和SpringFox 3.0,我们在@EnableSwagger2类中做了一些额外的事情,包括这里指定的内容:
http://springfox.github.io/springfox/docs/current/#changes-in-swagger-ui
对于非引导应用程序,springfox-swagger-ui不再通过添加依赖项自动启用。它需要使用资源处理程序配置器(WebFluxConfigurer或WebMvcConfigurer)显式注册。
这个例子是从那个页面复制的,我之前肯定是从那个页面得到的:

public class SwaggerUiWebMvcConfigurer implements WebMvcConfigurer {
  private final String baseUrl;

  public SwaggerUiWebMvcConfigurer(String baseUrl) {
    this.baseUrl = baseUrl;
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
    registry.
        addResourceHandler(baseUrl + "/swagger-ui/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
        .resourceChain(false);
  }

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController(baseUrl + "/swagger-ui/")
        .setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
  }
}

字符串
因此,虽然XML配置看起来是重复的,但如果使用@EnableWebMvc,* 也许 * 必须在Annotation配置中。
或者,为了在类中使用@Bean,类中也必须有@Configuration,这是我们设置中的另一个不同之处。
My Spring和SpringFox pom.xml摘录:

...
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>

        <spring.version>5.3.30</spring.version>
        <springfox.version>3.0.0</springfox.version>
    </properties>

    <dependencies>
...
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>${springfox.version}</version>
        </dependency>

    </dependencies>
...


我们的config类有这些注解:

@Configuration
@EnableWebMvc
@EnableSwagger2
@Import(springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class)
public class SpringFoxConfig implements WebMvcConfigurer {


(The最后一个是专门用于JSR-303验证感知的;基本操作不需要它。)
最后,我认为我们的单个Spring XML配置文件的唯一相关部分,它是通过DispatcherServletweb.xml文件加载的:

<context:annotation-config/>
    <bean class="our.app.package.api.swagger.SpringFoxConfig"/>


与您的示例可能相关的其他差异:

  • 我们没有任何自定义UiConfiguration bean
  • 我们在Docket构建器中使用自定义/有限路径选择器:.apis(RequestHandlerSelectors.basePackage("our.app.package"))

(如果后者有所作为,我会感到惊讶。)
最后,在应用程序启动时,您是否在服务器日志中看到这样的条目?(如果启用了调试日志记录。)
12:17:51,960 DEBUG _org.springframework.web.servlet.HandlerMapping.Mappings:'resourceHandlerMapping ' {/swagger-ui/**= ResourceHttpRequests [classpath [META-INF/resources/webjars/springfox-swagger-ui/]]}
对不起,我不能给给予一个更专业的,肯定的答案,但也许这至少有帮助。我至少可以确认非引导SpringMVC与SpringFox可以/确实工作。

6mzjoqzu

6mzjoqzu2#

解决了
对于任何感兴趣的人来说,事实证明我需要通过web.xml配置Servlet配置,因为我们当前的servlet是用<url-pattern>*.do</url-pattern>配置的,这阻止了我访问与swagger相关的URL。
解决我的问题的配置如下。
web.xml

<servlet>
        <servlet-name>swagger-ui</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/swagger-servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>swagger-ui</servlet-name>
        <url-pattern>/swagger-ui/index.html</url-pattern>
        <url-pattern>/swagger-ui/springfox.css</url-pattern>
        <url-pattern>/swagger-ui/swagger-ui.css</url-pattern>
        <url-pattern>/swagger-ui/springfox.js</url-pattern>
        <url-pattern>/swagger-ui/swagger-ui.js</url-pattern>
        <url-pattern>/swagger-ui/swagger-ui-bundle.js</url-pattern>
        <url-pattern>/swagger-ui/swagger-ui-standalone-preset.js</url-pattern>
        <url-pattern>/swagger-ui/favicon-32x32.png</url-pattern>
        <url-pattern>/swagger-ui/favicon-16x16.png</url-pattern>
        <url-pattern>/swagger-resources/configuration/ui</url-pattern>
        <url-pattern>/swagger-resources/configuration/security</url-pattern>
        <url-pattern>/swagger-resources</url-pattern>
        <url-pattern>/v2/api-docs</url-pattern>
    </servlet-mapping>

字符串
swagger-servlet-context.xml

<context:component-scan base-package="my.app.path.config, my.app.path.web.controllers" />
    
    <!-- cache resources to the browser-->
    <mvc:resources mapping="/swagger-ui/**" location="classpath:/META-INF/resources/webjars/springfox-swagger-ui/"/>

    <mvc:interceptors>
        <mvc:interceptor>
        <mvc:mapping path="/swagger-ui/**" />
        <mvc:mapping path="/v2/api-docs/**" />
        </mvc:interceptor>
    </mvc:interceptors>
    
    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven/>

相关问题