2017년 6월 19일 월요일

Spring swagger setting

We can set the swagger that can make your backEnd server's APIs documented.
There are many swagger integration tools,we are going to use spring fox project.

As default, In springFox, parse the request and the response of APIs,
and make the json response by using that.
You can find these json models by calling /v2/api-docs.

SpringFox also provide embedded swagger-ui.

First of all, you can set EnableSwagger2 annotation to enable /v2/api-docs API.

package org.blog.test.config;

import com.fasterxml.classmate.TypeResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by ugeun.park on 2017-06-17.
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket swaggerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/health/*"))
                .build();
    }
}

You can use embedded swagger-ui by setting springfox-swagger-ui as dependency.

compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'

- Execution result









You can download the example from the link as below.

https://gitlab.com/shashaka/spring-swagger-project

댓글 없음 :

댓글 쓰기