首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >springfox + swagger不工作

springfox + swagger不工作
EN

Stack Overflow用户
提问于 2018-05-17 17:00:47
回答 2查看 2.7K关注 0票数 1

我的pom.xml

代码语言:javascript
复制
<!-- Swagger io for API doc -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-core</artifactId>
            <version>1.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

Swagger配置

代码语言:javascript
复制
@Configuration
@EnableWebMvc
@EnableSwagger2
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("allocation-order-service")
                .description("domain services having persistance layer")
                .version("1.0")
                .build();
    }
}

服务器日志

代码语言:javascript
复制
16-05-18 13:08:59.137       ContainerBackgroundProcessor[StandardEngine[Catalina]]  INFO    o.s.w.s.m.m.a.RequestMappingHandlerMapping  registerHandlerMethod        190    Mapped "{[/${springfox.documentation.swagger.v2.path:/v2/api-docs}],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json || application/hal+json],custom=[]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)
16-05-18 13:08:59.141       ContainerBackgroundProcessor[StandardEngine[Catalina]]  INFO    o.s.w.s.m.m.a.RequestMappingHandlerMapping  registerHandlerMethod        190    Mapped "{[/swagger-resources/configuration/security],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
16-05-18 13:08:59.143       ContainerBackgroundProcessor[StandardEngine[Catalina]]  INFO    o.s.w.s.m.m.a.RequestMappingHandlerMapping  registerHandlerMethod        190    Mapped "{[/swagger-resources/configuration/ui],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
16-05-18 13:08:59.145       ContainerBackgroundProcessor[StandardEngine[Catalina]]  INFO    o.s.w.s.m.m.a.RequestMappingHandlerMapping  registerHandlerMethod        190    Mapped "{[/swagger-resources],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()

上面写着Mapped:/${springfox.documentation.swagger.v2.path:/v2/api-docs}

但这些都不起作用(404):

代码语言:javascript
复制
http://localhost:8080/allocation-order-web/v2/api-docs
http://localhost:8080/allocation-order-web/${springfox.documentation.swagger.v2.path:/v2/api-docs}

如果我使用spring-fox较低的版本,那么我会在日志中看到它映射了"{/v2/api-docs},methods=GET“,但是,我看不到任何生成的json。

EN

回答 2

Stack Overflow用户

发布于 2018-05-18 16:48:16

您可能需要指定主应用程序上下文路径以及swagger文档路径(最好在自定义属性文件或application.properties下定义它们):

代码语言:javascript
复制
@Configuration
@EnableWebMvc
@EnableSwagger2
@PropertySource("classpath:swagger-v2.properties")
public class SwaggerConfig {
    //...
}

swagger-v2.properties应该如下所示:

代码语言:javascript
复制
server.contextPath=/allocation-order-web/
springfox.documentation.swagger.v2.path=/api-docs

然后,可以通过以下方式访问文档:

票数 2
EN

Stack Overflow用户

发布于 2018-05-17 17:13:51

好了,这样就不管用了

代码语言:javascript
复制
http://localhost:8080/allocation-order-web/v2/api-docs

你也需要通过group attr,试试这个

代码语言:javascript
复制
http://localhost:8080/allocation-order-web/v2/api-docs?group=public-api
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50387396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档