zoaseo 2022. 10. 5. 15:46

1) 의존성 추가

pom.xml

  <!-- Swagger 2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

 

2) controller 추가

TestController.java

package com.hjseo.backend.swaggercontroller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class TestController {
    @GetMapping("user/search")
    public Map<String, String> search() {
        Map<String, String> response = new HashMap<String, String>();
        response.put("name", "taehong.kim");
        response.put("age", "28");
        response.put("email", "xxxxxxxx@gmail.com");
        return response;
    }
}

 

3) 클래스 생성

SwaggerConfig

package com.hjseo.backend;
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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hjseo.backend.swaggercontroller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(
                "TEST API",
                "Some custom description of API.",
                "0.0.1",
                "Terms of service",
                new Contact("MemoStack", "https://memostack.tistory.com", "public.devhong@gmail.com"),
                "License of API", "API license URL", Collections.emptyList());
    }
}

 

실행했더니

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

오류가 발생했다.

구글링해보니

이 파일에 한줄 추가하니 된다!!!

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

 

4) localhost:8080/swagger-ui.html  접속