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).
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" />
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
Serving Swagger requests right from our app
Spring MVC context (XML)
And finally we want to import that SwaggerConfig and handle Swagger UI (the HTML/CSS/JS files):