[JAVA] Spring Fox ① Introduction

Spring Fox ① Introduction

What is Spring Fox?

When used in combination with the Spring framework, it automatically generates API documents for the target system.

In addition, it is possible to suppress ** human mistakes ** and make documents with ** uniformity ** by ** automatically generating ** from the system.

What is Swagger

There are many points, so I'll list them ... φ (...)

Introduction

1. Add dependency

Add to gradle dependency

build.gradle


dependencies {
...
  compile "io.springfox:springfox-swagger2:2.2.2"
  compile "io.springfox:springfox-swagger-ui:2.2.2"
  compile "com.google.guava:guava:17.0"
...
}

2. Enable Spring Fox

Add and enable annotations

Application.java


@SpringBootApplication
@EnableSwagger2//add to
public class Application extends SpringBootServletInitializer
{
    public static void main( String... args )
    {
         SpringApplication.run( Application.class, args );
    }
}

3. Add JavaConfig for documentation

SwaggerConfiguration.java


@Configuration
public class SwaggerConfiguration
{
    @Bean
    public Docket publicDocument()
    {
        return new Docket( DocumentationType.SWAGGER_2 ).groupName( "public" )
                .select()
                .paths( paths() )
                .build()
                .apiInfo( apiInfo() );
    }
    
    private ApiInfo apiInfo()
    {
        ApiInfo apiInfo = new ApiInfo(
                "My REST API",
                "Some custom description of API.",
                "API TOS",
                "Terms of service",
                "[email protected]",
                "License of API",
                "API license URL");
        return apiInfo;
    }
    
    private Predicate<String> paths() {
        return or(
           regex("/some/endpoint")
        );
    }
    
}

4. Operation check

Access /swagger-ui.html

swagger_demo.png

It is convenient because the Java class of the return value is automatically converted to JSON SCHEMA and you can make a request from this screen.

next time

next

--How to add header parameters --Pull-down using DB value --Static documentation

I will touch it around.

Well then.

Recommended Posts

Spring Fox ① Introduction
Introduction to Ratpack (7) --Guice & Spring
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
Introduction to Spring Boot Part 1
Spring thorough introduction version upgrade memo
Lombok ① Introduction
Introduction (self-introduction)
[Introduction to Spring Boot] Form validation check
spring × docker
About Spring ③
Spring Java
[Java] Introduction
Introduction (editing)
Book introduction: Spring Boot Recommended reference book for beginners!
An introduction to Spring Boot + in-memory data grid
[Introduction to Spring Boot] Authentication function with Spring Security