Skip to content

Self-Descriptive Message와 HATEOAS를 만족하는 REST API를 구현 및 REST Docs를 통한 REST API 문서화

Notifications You must be signed in to change notification settings

dlcksgh1/REST-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST API - Event 생성, 조회, 수정

💻프로젝트 소개

  • Self-Descriptive Message와 HATEOAS를 만족하는 REST API를 구현
  • REST Docs를 통하여 REST API 문서화

📌 사용기술

  • 스프링 부트
  • 스프링 데이터 JPA
  • 스프링 HATEOAS
  • 스프링 REST Docs
  • 스프링 시큐리티 OAuth2
  • 스프링 REST Docs 구현

image

  • 엑세스 토큰발급

Authorization type 을 Basic Auth로 설정하고 client-id, client-secret를 입력

image

Body 에 username,password, grant_type 정보를 입력하여 요청하면 access_type이 bearer 인 access_token 정보를 받을 수있다.

image

  • 엑세스 토큰갱신

Authorization type 을 Basic Auth로 설정하고 client-id, client-secret를 입력해준다.

image

Body 에 grant_type에 refresh_token, refresh_token에 엑세스 토큰발급받을 때 같이 발급받았던 refresh_token에 정보를 입력하여 요청하면 access_type이 bearer 인 access_token 정보를 받을 수있다.

image

  • Api root 요청

API가 제공하는 resource 정보를 볼 수 있다.

image

  • Event 목록 조회

권한이 없더라도 목록조회는 가능

image

응답 예시

{
    "_embedded": {
        "eventList": [
            {
                "id": 3,
                "name": "event 0",
                "description": "test index 0",
                "beginEnrollmentDateTime": "2024-03-23T14:21:00",
                "closeEnrollmentDateTime": "2024-03-24T14:21:00",
                "beginEventDateTime": "2024-03-25T14:21:00",
                "endEventDateTime": "2024-03-26T14:21:00",
                "location": "강남역 D2 스타텁 팩토리",
                "basePrice": 100,
                "maxPrice": 200,
                "limitOfEnrollment": 100,
                "offline": true,
                "free": false,
                "eventStatus": "DRAFT",
                "manger": {
                    "id": 1
                },
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/events/3"
                    }
                }
            },

            ... 

            {
                "id": 22,
                "name": "event 19",
                "description": "test index 19",
                "beginEnrollmentDateTime": "2024-03-23T14:21:00",
                "closeEnrollmentDateTime": "2024-03-24T14:21:00",
                "beginEventDateTime": "2024-03-25T14:21:00",
                "endEventDateTime": "2024-03-26T14:21:00",
                "location": "강남역 D2 스타텁 팩토리",
                "basePrice": 100,
                "maxPrice": 200,
                "limitOfEnrollment": 100,
                "offline": true,
                "free": false,
                "eventStatus": "DRAFT",
                "manger": {
                    "id": 1
                },
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/events/22"
                    }
                }
            }
        ]
    },
    "_links": {
        "first": {
            "href": "http://localhost:8080/api/events?page=0&size=20"
        },
        "self": {
            "href": "http://localhost:8080/api/events?page=0&size=20"
        },
        "next": {
            "href": "http://localhost:8080/api/events?page=1&size=20"
        },
        "last": {
            "href": "http://localhost:8080/api/events?page=1&size=20"
        },
        "profile": {
            "href": "/docs/index.html#resources-events-list"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 30,
        "totalPages": 2,
        "number": 0
    }
}

로그인후 Event 목록 조회를 하게된다면 Event 생성 링크 제공한다.

image

응답 예시

{
    "_embedded": {
        "eventList": [
            {
                "id": 3,
                "name": "event 0",
                "description": "test index 0",
                "beginEnrollmentDateTime": "2024-03-23T14:21:00",
                "closeEnrollmentDateTime": "2024-03-24T14:21:00",
                "beginEventDateTime": "2024-03-25T14:21:00",
                "endEventDateTime": "2024-03-26T14:21:00",
                "location": "강남역 D2 스타텁 팩토리",
                "basePrice": 100,
                "maxPrice": 200,
                "limitOfEnrollment": 100,
                "offline": true,
                "free": false,
                "eventStatus": "DRAFT",
                "manger": {
                    "id": 1
                },
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/events/3"
                    }
                }
            },
            ...
            {
                "id": 10,
                "name": "event 7",
                "description": "test index 7",
                "beginEnrollmentDateTime": "2024-03-23T14:21:00",
                "closeEnrollmentDateTime": "2024-03-24T14:21:00",
                "beginEventDateTime": "2024-03-25T14:21:00",
                "endEventDateTime": "2024-03-26T14:21:00",
                "location": "강남역 D2 스타텁 팩토리",
                "basePrice": 100,
                "maxPrice": 200,
                "limitOfEnrollment": 100,
                "offline": true,
                "free": false,
                "eventStatus": "DRAFT",
                "manger": {
                    "id": 1
                },
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/events/10"
                    }
                }
            }
            
        ]
    },
    "_links": {
        "first": {
            "href": "http://localhost:8080/api/events?page=0&size=20"
        },
        "self": {
            "href": "http://localhost:8080/api/events?page=0&size=20"
        },
        "next": {
            "href": "http://localhost:8080/api/events?page=1&size=20"
        },
        "last": {
            "href": "http://localhost:8080/api/events?page=1&size=20"
        },
        "profile": {
            "href": "/docs/index.html#resources-events-list"
        },
        "create-event": {
            "href": "http://localhost:8080/api/events"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 30,
        "totalPages": 2,
        "number": 0
    }
}
  • Event 생성

권한추가 없이 Event 생성 요청을 하게되면 unauthorized 에러가 발생한다.

image

Bearer Token 추가 후 Event 생성 요청을 하게 되면 Event 가 정상적으로 생성 된 것을 확인할 수 있다.

image

Event 생성 응답 예시 (수정할 수있는 link 정보도 추가도 제공된다.)

{
    "id": 33,
    "name": "Spring",
    "description": "REST API Development with Spring",
    "beginEnrollmentDateTime": "2024-03-23T14:21:00",
    "closeEnrollmentDateTime": "2024-03-24T14:21:00",
    "beginEventDateTime": "2024-03-25T14:21:00",
    "endEventDateTime": "2024-03-26T14:21:00",
    "location": "강남역 D2 스타텁 팩토리",
    "basePrice": 100,
    "maxPrice": 200,
    "limitOfEnrollment": 100,
    "offline": true,
    "free": false,
    "eventStatus": "DRAFT",
    "manger": null,
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/events/33"
        },
        "query-events": {
            "href": "http://localhost:8080/api/events"
        },
        "update-event": {
            "href": "http://localhost:8080/api/events/33"
        },
        "profile": {
            "href": "/docs/index.html#resources-events-create"
        }
    }
}
  • Event 조회

로그인 없이 Event 조회

image

로그인 했을 때 로그인 사용자가 Event Manager 인 경우에 Event 수정할 수 있는 link를 제공한다.

image

About

Self-Descriptive Message와 HATEOAS를 만족하는 REST API를 구현 및 REST Docs를 통한 REST API 문서화

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages