Skip to content
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

SpringMVC with OpenAPI 3.0 and Swagger #11

Open
ctapobep opened this issue Aug 28, 2022 · 0 comments
Open

SpringMVC with OpenAPI 3.0 and Swagger #11

ctapobep opened this issue Aug 28, 2022 · 0 comments

Comments

@ctapobep
Copy link
Owner

ctapobep commented Aug 28, 2022

Here's a setup of Swagger with Spring MVC without Spring Boot. Note that Swagger had its static resources in a different folder in previous versions (before version 3.0.0).

Maven

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

Serving Swagger requests right from our app

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.ant;

@Configuration
// Too much to dig through the code if we go w/o this annotation - it does package-scan and imports other
// contexts that do package-scan too. On top of that there's an auto-configuration which depends on the
// presence of other entities. It's just ugh, you can't easily figure out what's actually initialized.
// Kids, don't write libraries like Swagger team - code must be simple and explicit!
@EnableSwagger2
class SwaggerConfig {

    /**
     * Swagger creates an endpoint that returns JSON with all the info about our API that it deduced from
     * SpringMVC. This doesn't yet handle HTML/CSS/JS resources though - we set up Spring MVC to handle them.
     */
    @Bean Docket swagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(ant("/api/**"))
                .build();
    }
}

Spring MVC context (XML)

And finally we want to import that SwaggerConfig and handle Swagger UI (the HTML/CSS/JS files):

<!-- Swagger keeps HTML/CSS/JS resources in its jar file in META-INF folder. swagger-ui/ is the default context
     path where Swagger API resides. -->
<mvc:resources mapping="/swagger-ui/**" location="classpath:/META-INF/resources/webjars/springfox-swagger-ui/"/>
<context:component-scan base-package="io.elsci.moleve.web" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant