New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger support for Spring Boot 2 / Spring 5? #2155

Closed
springframeworkguru opened this Issue Dec 17, 2017 · 18 comments

Comments

Projects
None yet
5 participants
@springframeworkguru

springframeworkguru commented Dec 17, 2017

I'm getting a 404 on the Swagger UI url with a Spring Boot 2 (M7) project. Is there any config I need to do for Spring Boot 2 / Spring 5 (Spring MVC. not using Reactive).

Here's my project: https://github.com/springframeworkguru/spring5-mvc-rest/tree/vendor-api

You'll see its a pretty simple project (no Spring Sec).

I added deps for the 2.7.0 version and the following config:

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("guru.springframework"))
                .paths(regex("/api/v1/*"))
                .build();
    }
}

The JSON endpoint works fine. Only the Swagger UI endpoint is returning a 404.

Thanks!

@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Dec 18, 2017

@springframeworkguru spring boot 2 and spring fx 5 aren't really supported as of now. Trying to get the next release (2.8.0) in before starting to work on it. Let me look at your repo and see what the issue might be.

@springframeworkguru

This comment has been minimized.

springframeworkguru commented Dec 18, 2017

Thanks @dilipkrish! Appreciate the help.

I just committed my changes on this branch.

@springframeworkguru

This comment has been minimized.

springframeworkguru commented Dec 18, 2017

@dilipkrish - Got it working. Something broke the config in Spring 5 / Spring Boot. I had to add config for resource handlers.

For others hitting this issue:

Note: WebMvcConfigurerAdapter has been deprecated in Spring 5.

@EnableSwagger2
@Configuration
public class SwaggerConfig extends WebMvcConfigurationSupport {

    public Docket api(){

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("guru.springframework"))
                .paths(regex("/api/v1/*"))
                .build();

    }

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

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}```
@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Dec 18, 2017

Oh cool! So in your experience everything related to traditional mvc works as expected?

@springframeworkguru

This comment has been minimized.

springframeworkguru commented Dec 18, 2017

I think so. Seems only Swagger UI was broken. With the above config, Swagger UI is looking good.

I'm developing a section on Swagger for my Spring 5 course. So, I'll be getting into the various config options over the next few days. Fingers crossed!

@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Dec 19, 2017

Thats encouraging! So the new functional endpoints are working as well? I'll update this post when 2.8.0 is released (hoping soon enough!)

@springframeworkguru

This comment has been minimized.

springframeworkguru commented Dec 19, 2017

Have not tested functional endpoints - not yet!

@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Dec 19, 2017

For a moment there I was excited! sigh!

@Aloren

This comment has been minimized.

Aloren commented Dec 22, 2017

In our project Swagger 2.7.0 works correctly with Spring Boot 2.0.0.M3 without adding resource handlers.

@springframeworkguru

This comment has been minimized.

springframeworkguru commented Dec 22, 2017

I saw that yesterday - I ran through it again, I wasn't able to recreate it. I reviewed the code changes & could not spot the delta. No idea what I was doing to cause it... I was working with M7. Going to close this issue. Thanks!

@wafflebot wafflebot bot removed the current label Dec 22, 2017

@dilipkrish dilipkrish added this to the 2.8.0 milestone Dec 28, 2017

@armdev

This comment has been minimized.

armdev commented Mar 2, 2018

Swagger2 does not working with Spring Boot 2 when we add embedded reactive connectors(netty). Because Swagger has dependency from Spring web MVC, and if you exclude tomcat, swagger will fail. Just tested with Spring Boot 2.

@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Mar 5, 2018

@armdev thanks for letting us know!

@adriens

This comment has been minimized.

adriens commented Mar 21, 2018

Hi @springframeworkguru , i'm running Spring Boot 2, and trying to make run Swagger 2 on top of it.
Thanks to your tutorial, i'm able to get the json up anr running, when, even with the code you posted in this issue, i'm not able to get the html up, i get the following log telling me th htm is not found :

`2018-03-22 07:05:12.280 WARN 1928 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/v2/api-docs/swagger-ui.html] in DispatcherServlet with name 'dispatcherServlet'

To make short :

http://localhost:8080/v2/api-docs -> OK, looks perfect !
http://localhost:8080/v2/api-docs/swagger-ui.html -> 404

I've used the code provided by @springframework guru in this ticket.

Any idea what i should to to make it up and running please ?...it's so frustrating not to see the html ;-p

Thanks in advance for any help. I'm on the 2.8.0

@adriens

This comment has been minimized.

adriens commented Mar 21, 2018

I was initially using the following tutorial (bfefore to switch on this issue) : https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/

@armdev

This comment has been minimized.

armdev commented Mar 21, 2018

I pushed project with Spring boot 2 and swagger https://github.com/armdev/springboot2-swagger , but application configured as a "servlet", not a "reactive",
https://github.com/armdev/springboot2-swagger/blob/master/src/main/java/io/project/app/Application.java
but reactive stack is working.

@adriens

This comment has been minimized.

adriens commented Mar 21, 2018

Thanks a lot @armdev , i'm taking a look at it, i'll let you know as soon as it works 👍

@adriens

This comment has been minimized.

adriens commented Mar 21, 2018

I've just run your code locally without any problem.

just

git clone https://github.com/armdev/springboot2-swagger.git
cd springboot2-swagger.git
mvn spring-boot:run
firefox localhost:4545/swagger-ui.html

and swagger-ui is up an running. Ill use htis as a tutorial now.

Thanks a lot @armdev for great help.

@adriens

This comment has been minimized.

adriens commented Mar 29, 2018

@armdev : your code allowed me to make swagger-ui work with only few lines of code on the main app 👍 💯
Thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment