Springfox + Swagger не работает - PullRequest
0 голосов
/ 17 мая 2018

my pom.xml

<!-- 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 config

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

серверные логи

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):

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, то я получу в своих журналах, что она имеет Mapped "{[/ v2 / api-docs}], method = [GET]. Однако я не могу видеть какие-либо JSON генерируется там.

Ответы [ 2 ]

0 голосов
/ 18 мая 2018

Вам может потребоваться указать основной путь к контексту приложения вместе с путем к документации swagger (вам лучше определить их в файле пользовательских свойств или в application.properties ):

@Configuration
@EnableWebMvc
@EnableSwagger2
@PropertySource("classpath:swagger-v2.properties")
public class SwaggerConfig {
    //...
}

swagger-v2.properties должен выглядеть следующим образом:

server.contextPath=/allocation-order-web/
springfox.documentation.swagger.v2.path=/api-docs

Документация должна быть доступна через:

http://localhost:8080/allocation-order-web/api-docs

0 голосов
/ 17 мая 2018

Ну просто это не сработает

http://localhost:8080/allocation-order-web/v2/api-docs

вам нужно также передать групповой атрибут, попробуйте это

http://localhost:8080/allocation-order-web/v2/api-docs?group=public-api
...