반응형

Spring Boot : Swagger UI 활용 (2) - spring fox 추가

spring fox 라이브러리의 Swagger UI를 활용하여 spring boot 프로젝트에 매핑된 controller 테스트

 

  • Swagger : 정의, 활용
  • 개발환경 : Java, Maven, Spring-fox
  • 라이브러리 다운로드 (pom.xml)
  • 설정 : application.properties / application.yml
  • Hello World : 기본 호출 테스트 - default controller
  • swagger 설정 추가
  • 문자열 List 반환 테스트

 

 

# 개발환경

 

자바 Java OpenJDK 1.8.0_292
Spring Spring-Boot 2.4.5
편집기 IDE IntelliJ
빌드/의존성 관리 Build / Library Management Maven

 

 

# 프로젝트 구조

 

 

# 라이브러리 다운로드 (pom.xml)

 

spring fox dependency 추가

1
2
3
4
5
6
7
8
9
10
11
<!-- Spring Fox Dependencies -->
<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>

 

더보기

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>home.test.springfox</groupId>
    <artifactId>test-springfox</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>test-springfox</name>
    <description>Spring Boot project for swagger test(spring fox dependency)</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!-- <scope>provided</scope>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
 
        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
 
        <!-- Spring Fox Dependencies -->
        <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>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
 
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>
 

 

 

# 설정 (application.properties)

 

1
2
3
4
###### Server Properties  ###########################################
#server.port=9091
server.port=8081
 

 

 

# Hello World (기본 호출 테스트 - default controller)

[ DefaultController.java ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package home.test.springfox.web;
 
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
 
@RestController
@RequestMapping
public class DefaultController {
    
    @GetMapping(value = "/")
    public RedirectView redirectView(RedirectAttributes attributes) {
        return new RedirectView("/api/test");
    }
 
    @GetMapping(value = "/api/test")
    public ResponseEntity<String> testApiCall() {
        return ResponseEntity.ok("Hello World!");
    }
 
}
 

 

 

 

# 문자열 List 반환 테스트

[ SampleController.java ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package home.test.springfox.web;
 
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.List;
 
@RestController
@RequestMapping(value = "/api/samples")
public class SampleController {
 
    @GetMapping
    public ResponseEntity<List<String>> findAllSamples() {
        List<String> sampleList = new ArrayList<>();
        for (int i = 0; i < 9; i++) sampleList.add("Sample " + i);
        return ResponseEntity.status(HttpStatus.OK).body(sampleList);
    }
 
}
 

 

 

반응형