前提・実現したいこと
OAuthで認証しているGitHubアカウントの、リポジトリ名・ユーザー情報を表示するページを作ろうとしています。
リポジトリ名は取り出せたのですが、リポジトリのユーザー情報がネストされていて(下記参照)、どう取り出したら良いのかわかりません。
例えば"owner"内の"login"を取り出したい場合、どのように記述すれば良いでしょうか?
解決策がありましたら、アドバイスをお願いします。
GitHubApi
1//一部抜粋 2[ 3 { 4 "id": 00000, 5 "node_id": "qwerty", 6 "name": "sample", 7 "full_name": "hogehoge/sample", 8 "private": false, 9 "owner": { 10 "login": "hogehoge",//ここを取り出したい 11 "id": 000000, 12 "node_id": "qwerty", 13 "avatar_url": "https://avatars2.githubusercontent.com/u/39675084?v=4", 14 "gravatar_id": "", 15 "url": "https://api.github.com/users/hogehoge", 16 "html_url": "https://github.com/hogehoge", 17 "followers_url": 18 //中略 19 "type": "User", 20 "site_admin": false 21 }, 22 "html_url": "https://github.com/hogehoge/sample", 23 "description": null, 24 "fork": false, 25 //以下省略
該当のソースコード
Entity
1package com.example.restapi.entity; 2 3~ 4 5@Entity 6public class OAuth { 7 8 @Id 9 private Long id; 10 11 private String name; 12 13 //これだと取得できませんでした 14 @JsonProperty("owner.login") 15 private String login; 16 17 @JsonProperty("html_url") 18 private String htmlUrl; 19 20 private String created_at; 21 22 //Getter.Setter略 23} 24 25
controller
1package com.example.restapi.controller; 2 3~ 4 5@Controller 6public class OAuthController { 7 8 @Autowired 9 private OAuth2RestTemplate auth2RestTemplate; 10 11 @GetMapping("/repository") 12 public String repositories(Model model){ 13 URI repos = UriComponentsBuilder.fromUriString("https://api.github.com/user/repos").build().toUri(); 14 model.addAttribute("repos",auth2RestTemplate.getForEntity(repos, OAuth[].class).getBody()); 15 return "repository"; 16 } 17} 18
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" 3 xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> 4<head> 5 <meta charset="utf-8"> 6 <title>Repos</title> 7</head> 8<body> 9 10//ここにユーザー情報を表示させたい 11<div th:each="usr : ${repos}" th:object="${usr}"> 12 <p th:text="*{login}"></p> 13</div> 14 15<ul> 16 <li th:each="repo : ${repos}" th:object="${repo}"> 17 <a th:text="*{name}" th:href="*{htmlUrl}"></a> 18 <p th:text="*{created_at}"></p> 19 </li> 20</ul> 21 22</body> 23 24 25</html>
試したこと
Entityの @JsonProperty("owner.login")部分の表記を色々変えて試してみましたが、取得できませんでした。
エラーにはなりませんが何も表示されない状態です。
使用しているツールなど
・SpringBoot
・SpringSecurity
・gradle
・OAuth2
・thymeleaf
・IntelliJ IDEA
あなたの回答
tips
プレビュー