jpganz18
jpganz18

Reputation: 5858

swagger page is empty,

When I dont have @EnableWebMvc I can access

http://localhost:8080/swagger-ui.html

But looks like this:

enter image description here and at my network I have this 404 request enter image description here

When I enable I cannot access to that url (seem like doesnt exist)

at my startup logs I only see

 Mapped "{[/custom],methods=[GET]}" onto java.lang.String com.example.demo.web.rest.HelloController.hi()
2017-08-08 15:27:29.951  INFO 94164 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/u],methods=[GET]}" onto java.lang.String com.example.demo.web.rest.HelloController.home()
2017-08-08 15:27:29.952  INFO 94164 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" 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)
2017-08-08 15:27:29.955  INFO 94164 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/ui]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
2017-08-08 15:27:29.956  INFO 94164 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/security]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
2017-08-08 15:27:29.956  INFO 94164 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources]}" onto org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
2017-08-08 15:27:29.959  INFO 94164 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-08 15:27:29.959  INFO 94164 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-08 15:27:30.137  INFO 94164 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter

My config file is this:

@Configuration
//@EnableWebMvc
@EnableSwagger2
public class WebConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.web.rest"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(metaData());
    }

    private ApiInfo metaData() {
        ApiInfo apiInfo = new ApiInfo(
                "Spring Boot REST API",
                "Spring Boot REST API for Online Store",
                "1.0",
                "Terms of service",
                "someone",
                "Apache License Version 2.0",
                "https://www.apache.org/licenses/LICENSE-2.0");
        return apiInfo;
    }

}

and my main is:

@Configuration
@ComponentScan
@EnableAutoConfiguration

public class DemoApplication {


    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

and at my pom.xml

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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.4.0</version>
        </dependency>

Any idea whats wrong?

Upvotes: 0

Views: 4875

Answers (3)

tudor
tudor

Reputation: 161

If your WebConfig is in a different package than the DemoApplication class you may want to add the package to basePackages in @ComponentScan

Upvotes: 1

lzagkaretos
lzagkaretos

Reputation: 2910

If your application has defined a custom contextPath (server.contextPath in application.properties), then it has to be included in the url. http://localhost:8080/contextPath/swagger-ui.html

You could also try to change RequestHandlerSelectors.basePackage("com.example.demo.web.rest") to RequestHandlerSelectors.any() and see what happens.

Upvotes: 0

mad_fox
mad_fox

Reputation: 3188

Have your resouceHandler?

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/swagger/");
    }

I have this in a class that extends WebMvcConfigurerAdapter

Upvotes: 0

Related Questions