Step by step teach you SSM integration swagger

Keywords: Web Development Maven Tomcat Java Spring

First: What is swagger?

swagger is a very useful framework for writing API documents. Other self-owned Baidu

Second: ssm integration swagger?

1: Introduce dependencies in maven's pom file: (Note version, otherwise tomcat will not start properly)

<!-- Introduce swagger -->
        <!--springfox The core of jar package -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--springfox-ui Of jar package(It contains swagger Interface static files) -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--springfox Dependent jar Package; if your project has been integrated without duplication -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>

2: Create SwaggerConfig.java file, that is, swagger configuration file, preferably placed in a separate folder

@WebAppConfiguration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages="com.zgz.cn.controller")
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx Project Interface Document")
                .description("xxx Project Interface Testing")
                .version("1.0.0")
                .termsOfServiceUrl("")
                .license("")
                .licenseUrl("")
                .build();
    }
}

3: Configure in the Spring MVC configuration file: (Configure the package where all controllers are located)

<!-- To configure swagger Of bean -->
    <!-- Delegate static resources to the default servlet Handle -->
    <mvc:default-servlet-handler/>
    <!-- Automatic Injection Configuration to Containers -->
    <context:annotation-config/>
    <!-- take SwaggerConfig Configuration class injection -->
    <bean class="com.zgz.cn.swagger.SwaggerConfig"/>
    <!-- To configure swagger Resources are not intercepted -->
<!--    <bean id="swagger2Config" class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration"/> -->
    <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
    <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>

4: Configuring all requests in web.xml is handled by Dispatcher Servlet

<servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

5: Use annotations in the controller: (1) Common annotations for swagger)

6: Test (access address: project name / swagger-ui.html)

Posted by abasel on Sat, 26 Jan 2019 00:57:14 -0800