DEV Community

Özgür Akıncı
Özgür Akıncı

Posted on

Spring Boot SwaggerUI 2 Entegrasyonu

Swagger nedir?
API methodlarının dokumante edilmesine olanak sağlayan bir rest api arayüzüdür.

Spring Boot SwaggerUI Etegrasyonu

pom.xml

<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>2.9.2</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

SpringSwaggerConfig.java

package com.app.learn_spring;

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

@Configuration
@EnableSwagger2
public class SpringSwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.app.learn_spring"))
                .paths(PathSelectors.any())
                .build().apiInfo(apiEndPointsInfo());

    }
    private ApiInfo apiEndPointsInfo() {
        return new ApiInfoBuilder().title("Spring Swagger API Documentation")
                .description("Spring Swagger API Documentation")
                .build();
    }
}
Enter fullscreen mode Exit fullscreen mode

application.properties

spring.mvc.pathmatch.matching-strategy=ant-path-matcher
Enter fullscreen mode Exit fullscreen mode

Örnek Rest Controller

package com.app.learn_spring.controller;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value = "CustomController Api Doc")
public class CustomController {

    @RequestMapping(value = "/post_method_example", method = RequestMethod.POST)
    public String post() {
        return "post";
    }

    @RequestMapping(value = "/get_method_example", method = RequestMethod.GET)
    public String get() {
        return "get";
    }

    @RequestMapping(value = "/put_method_example", method = RequestMethod.PUT)
    public String put() {
        return "put";
    }

    @RequestMapping(value = "/delete_method_example", method = RequestMethod.DELETE)
    public String delete() {
        return "delete";
    }
}
Enter fullscreen mode Exit fullscreen mode

Uygulama Görseli (http://localhost:8080/swagger-ui.html/)
Image description

Top comments (0)