관리 메뉴

너와 나의 스토리

[Spring] Json 데이터 객체로 받기, Github Webhook 파싱하기 본문

개발/Spring Boot

[Spring] Json 데이터 객체로 받기, Github Webhook 파싱하기

노는게제일좋아! 2021. 3. 14. 16:26
반응형

Json 데이터를 받을 때, 일부 필요한 데이터만 추출해서 받고 싶을 때가 있다.

{
  "zen": "Mind your words, they are important.",
  "hook_id": 283587005,
  "hook": {
    "type": "Repository",
    "id": 283587005,
    "name": "web",
    "active": true,
    "events": [
      "push"
    ],
    "config": {
      "content_type": "json",
      "insecure_ssl": "0",
      "url": "http://192.168.0.5:8080/job"
    },
    "updated_at": "2021-02-26T05:16:45Z",
    "created_at": "2021-02-26T05:16:45Z",
    "url": "https://api.github.com/repos/hovy1994/Spring-Batch/hooks/283587005",
    "test_url": "https://api.github.com/repos/hovy1994/Spring-Batch/hooks/283587005/test",
    "ping_url": "https://api.github.com/repos/hovy1994/Spring-Batch/hooks/283587005/pings",
    "last_response": {
      "code": null,
      "status": "unused",
      "message": null
    }
  },
  "repository": {
    "id": 331223912,
    "node_id": "MDEwOlJlcG9zaXRvcnkzMzEyMjM5MTI=",
    "name": "Spring-Batch",
    "full_name": "hovy1994/Spring-Batch",
    "private": true,
    "owner": {
      "login": "hovy1994",
      "id": 37677421,
      "node_id": "MDQ6VXNlcjM3Njc3NDIx",
      "avatar_url": "https://avatars.githubusercontent.com/u/37677421?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/hovy1994",
      "html_url": "https://github.com/hovy1994",
      "followers_url": "https://api.github.com/users/hovy1994/followers",
      "following_url": "https://api.github.com/users/hovy1994/following{/other_user}",
      "gists_url": "https://api.github.com/users/hovy1994/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/hovy1994/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/hovy1994/subscriptions",
      "organizations_url": "https://api.github.com/users/hovy1994/orgs",
      "repos_url": "https://api.github.com/users/hovy1994/repos",
      "events_url": "https://api.github.com/users/hovy1994/events{/privacy}",
      "received_events_url": "https://api.github.com/users/hovy1994/received_events",
      "type": "User",
      "site_admin": false
    },
    "html_url": "https://github.com/hovy1994/Spring-Batch",
    "description": null,
    "fork": false,
    "url": "https://api.github.com/repos/hovy1994/Spring-Batch",
    "forks_url": "https://api.github.com/repos/hovy1994/Spring-Batch/forks",
    "keys_url": "https://api.github.com/repos/hovy1994/Spring-Batch/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/hovy1994/Spring-Batch/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/hovy1994/Spring-Batch/teams",
    "hooks_url": "https://api.github.com/repos/hovy1994/Spring-Batch/hooks",
    "issue_events_url": "https://api.github.com/repos/hovy1994/Spring-Batch/issues/events{/number}",
    "events_url": "https://api.github.com/repos/hovy1994/Spring-Batch/events",
    "assignees_url": "https://api.github.com/repos/hovy1994/Spring-Batch/assignees{/user}",
    "branches_url": "https://api.github.com/repos/hovy1994/Spring-Batch/branches{/branch}",
    "tags_url": "https://api.github.com/repos/hovy1994/Spring-Batch/tags",
    "blobs_url": "https://api.github.com/repos/hovy1994/Spring-Batch/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/hovy1994/Spring-Batch/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/hovy1994/Spring-Batch/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/hovy1994/Spring-Batch/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/hovy1994/Spring-Batch/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/hovy1994/Spring-Batch/languages",
    "stargazers_url": "https://api.github.com/repos/hovy1994/Spring-Batch/stargazers",
    "contributors_url": "https://api.github.com/repos/hovy1994/Spring-Batch/contributors",
    "subscribers_url": "https://api.github.com/repos/hovy1994/Spring-Batch/subscribers",
    "subscription_url": "https://api.github.com/repos/hovy1994/Spring-Batch/subscription",
    "commits_url": "https://api.github.com/repos/hovy1994/Spring-Batch/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/hovy1994/Spring-Batch/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/hovy1994/Spring-Batch/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/hovy1994/Spring-Batch/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/hovy1994/Spring-Batch/contents/{+path}",
    "compare_url": "https://api.github.com/repos/hovy1994/Spring-Batch/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/hovy1994/Spring-Batch/merges",
    "archive_url": "https://api.github.com/repos/hovy1994/Spring-Batch/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/hovy1994/Spring-Batch/downloads",
    "issues_url": "https://api.github.com/repos/hovy1994/Spring-Batch/issues{/number}",
    "pulls_url": "https://api.github.com/repos/hovy1994/Spring-Batch/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/hovy1994/Spring-Batch/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/hovy1994/Spring-Batch/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/hovy1994/Spring-Batch/labels{/name}",
    "releases_url": "https://api.github.com/repos/hovy1994/Spring-Batch/releases{/id}",
    "deployments_url": "https://api.github.com/repos/hovy1994/Spring-Batch/deployments",
    "created_at": "2021-01-20T07:15:00Z",
    "updated_at": "2021-01-22T05:04:08Z",
    "pushed_at": "2021-01-22T05:04:06Z",
    "git_url": "git://github.com/hovy1994/Spring-Batch.git",
    "ssh_url": "git@github.com:hovy1994/Spring-Batch.git",
    "clone_url": "https://github.com/hovy1994/Spring-Batch.git",
    "svn_url": "https://github.com/hovy1994/Spring-Batch",
    "homepage": null,
    "size": 70,
    "stargazers_count": 0,
    "watchers_count": 0,
    "language": "Java",
    "has_issues": true,
    "has_projects": true,
    "has_downloads": true,
    "has_wiki": true,
    "has_pages": false,
    "forks_count": 0,
    "mirror_url": null,
    "archived": false,
    "disabled": false,
    "open_issues_count": 0,
    "license": null,
    "forks": 0,
    "open_issues": 0,
    "watchers": 0,
    "default_branch": "develop"
  },
  "sender": {
    "login": "hovy1994",
    "id": 37677421,
    "node_id": "MDQ6VXNlcjM3Njc3NDIx",
    "avatar_url": "https://avatars.githubusercontent.com/u/37677421?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/hovy1994",
    "html_url": "https://github.com/hovy1994",
    "followers_url": "https://api.github.com/users/hovy1994/followers",
    "following_url": "https://api.github.com/users/hovy1994/following{/other_user}",
    "gists_url": "https://api.github.com/users/hovy1994/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/hovy1994/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/hovy1994/subscriptions",
    "organizations_url": "https://api.github.com/users/hovy1994/orgs",
    "repos_url": "https://api.github.com/users/hovy1994/repos",
    "events_url": "https://api.github.com/users/hovy1994/events{/privacy}",
    "received_events_url": "https://api.github.com/users/hovy1994/received_events",
    "type": "User",
    "site_admin": false
  }
}
  • 보통 이 많은 정보를 다 사용하지 않으니, 몇 가지 필요한 데이터만 뽑아서 객체로 만들어 보자.
    • 필요한 정보: 사용자 아이디, repository 이름, clone할 url
  • 다음과 같이 작성해주면 된다.

  • 실제 들어오는 이름과 내가 객체에서 사용하고 싶은 이름이 다르면 예제와 같이 @JsonProperty 어노테이션을 이용해서 정의해주면 된다.

Controller
Console

 

반응형
Comments