问题描述
如标题所示,我需要将 JSON 文件转换为 java 对象。我正在寻找解决此问题的 Spring 或 Java 解决方案。
这是我的控制器类(稍后会重构)
package pl.githubdetails.GithubDetails.presentation;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pl.githubdetails.GithubDetails.applcation.RepositoryDetailsDTO;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.text.MessageFormat;
@RestController
@RequestMapping("/repositories")
public class DetailsController
{
final String uriTemplate = "https://api.github.com/repos/{0}/{1}";
@GetMapping(value = "/{owner}/{repositoryName}")
RepositoryDetailsDTO getRepositoryDetails(@PathVariable String owner,@PathVariable String repositoryName) throws IOException,InterruptedException {
RepositoryDetailsDTO repositoryDetailsDTO = new RepositoryDetailsDTO();
String uri = MessageFormat.format(uriTemplate,owner,repositoryName);
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(uri))
.build();
HttpResponse<String> httpResponse = httpClient.send(request,HttpResponse.BodyHandlers.ofString());
System.out.println(httpResponse.body());
return null;
}
}
JSON 响应示例:
[
{
"id": 1296269,"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5","name": "Hello-World","full_name": "octocat/Hello-World","owner": {
"login": "octocat","id": 1,"node_id": "MDQ6VXNlcjE=","avatar_url": "https://github.com/images/error/octocat_happy.gif","gravatar_id": "","url": "https://api.github.com/users/octocat","html_url": "https://github.com/octocat","followers_url": "https://api.github.com/users/octocat/followers","following_url": "https://api.github.com/users/octocat/following{/other_user}","gists_url": "https://api.github.com/users/octocat/gists{/gist_id}","starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/octocat/subscriptions","organizations_url": "https://api.github.com/users/octocat/orgs","repos_url": "https://api.github.com/users/octocat/repos","events_url": "https://api.github.com/users/octocat/events{/privacy}","received_events_url": "https://api.github.com/users/octocat/received_events","type": "User","site_admin": false
},"private": false,"html_url": "https://github.com/octocat/Hello-World","description": "This your first repo!","fork": false,"url": "https://api.github.com/repos/octocat/Hello-World","archive_url": "https://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}","assignees_url": "https://api.github.com/repos/octocat/Hello-World/assignees{/user}","blobs_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}","branches_url": "https://api.github.com/repos/octocat/Hello-World/branches{/branch}","collaborators_url": "https://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}","comments_url": "https://api.github.com/repos/octocat/Hello-World/comments{/number}","commits_url": "https://api.github.com/repos/octocat/Hello-World/commits{/sha}","compare_url": "https://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}","contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/{+path}","contributors_url": "https://api.github.com/repos/octocat/Hello-World/contributors","deployments_url": "https://api.github.com/repos/octocat/Hello-World/deployments","downloads_url": "https://api.github.com/repos/octocat/Hello-World/downloads","events_url": "https://api.github.com/repos/octocat/Hello-World/events","forks_url": "https://api.github.com/repos/octocat/Hello-World/forks","git_commits_url": "https://api.github.com/repos/octocat/Hello-World/git/commits{/sha}","git_refs_url": "https://api.github.com/repos/octocat/Hello-World/git/refs{/sha}","git_tags_url": "https://api.github.com/repos/octocat/Hello-World/git/tags{/sha}","git_url": "git:github.com/octocat/Hello-World.git","issue_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments{/number}","issue_events_url": "https://api.github.com/repos/octocat/Hello-World/issues/events{/number}","issues_url": "https://api.github.com/repos/octocat/Hello-World/issues{/number}","keys_url": "https://api.github.com/repos/octocat/Hello-World/keys{/key_id}","labels_url": "https://api.github.com/repos/octocat/Hello-World/labels{/name}","languages_url": "https://api.github.com/repos/octocat/Hello-World/languages","merges_url": "https://api.github.com/repos/octocat/Hello-World/merges","milestones_url": "https://api.github.com/repos/octocat/Hello-World/milestones{/number}","notifications_url": "https://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}","pulls_url": "https://api.github.com/repos/octocat/Hello-World/pulls{/number}","releases_url": "https://api.github.com/repos/octocat/Hello-World/releases{/id}","ssh_url": "[email protected]:octocat/Hello-World.git","stargazers_url": "https://api.github.com/repos/octocat/Hello-World/stargazers","statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/{sha}","subscribers_url": "https://api.github.com/repos/octocat/Hello-World/subscribers","subscription_url": "https://api.github.com/repos/octocat/Hello-World/subscription","tags_url": "https://api.github.com/repos/octocat/Hello-World/tags","teams_url": "https://api.github.com/repos/octocat/Hello-World/teams","trees_url": "https://api.github.com/repos/octocat/Hello-World/git/trees{/sha}","clone_url": "https://github.com/octocat/Hello-World.git","mirror_url": "git:git.example.com/octocat/Hello-World","hooks_url": "https://api.github.com/repos/octocat/Hello-World/hooks","svn_url": "https://svn.github.com/octocat/Hello-World","homepage": "https://github.com","language": null,"forks_count": 9,"stargazers_count": 80,"watchers_count": 80,"size": 108,"default_branch": "master","open_issues_count": 0,"is_template": false,"topics": [
"octocat","atom","electron","api"
],"has_issues": true,"has_projects": true,"has_wiki": true
我想把它转换成这样一个类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RepositoryDetailsDTO
{
String full_name;
String description;
String cloneUrl;
String stars;
String createdAt;
}
你知道我该怎么做吗?我正在寻找执行此操作的最佳方法。感谢您的回答。顺便说一句,http 客户端此时运行良好,但仅适用于字符串响应。我尝试过类似
HttpResponse<RepositoryDetailsDTO> httpResponse = httpClient.send(request,HttpResponse.BodyHandlers.replacing(repositoryDetailsDTO));
但是没有用。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)