Jackson을 사용하여 json 문자열을 역 직렬화 할 때 종종 모든 빈 클래스의 속성을 만들고 싶지 않으며 json 문자열의 필드가 필요하고 필요하지 않은 다른 필드가 필요합니다. 그래서 나는 종종 내가 필요한 자바 클래스 빈에 몇 가지 속성을 작성합니다. 그러나 Jackson이 그것을 파싱하면 빈 필드에 대해 null로 반환됩니다.
1. Java Bean 클래스는 다음 GistObject
과 같습니다.
public class GistObject {
private String id;
}
jackson Main 클래스 코드는 다음과 같습니다.
String json =
" {\n" +
" \"url\": \"https://api.github.com/gists/e69fd9f9ef85eb3f30a3b93d2cc9b9b3\",\n" +
" \"forks_url\": \"https://api.github.com/gists/e69fd9f9ef85eb3f30a3b93d2cc9b9b3/forks\",\n" +
" \"commits_url\": \"https://api.github.com/gists/e69fd9f9ef85eb3f30a3b93d2cc9b9b3/commits\",\n" +
" \"id\": \"e69fd9f9ef85eb3f30a3b93d2cc9b9b3\",\n" +
" \"git_pull_url\": \"https://gist.github.com/e69fd9f9ef85eb3f30a3b93d2cc9b9b3.git\",\n" +
" \"git_push_url\": \"https://gist.github.com/e69fd9f9ef85eb3f30a3b93d2cc9b9b3.git\",\n" +
" \"html_url\": \"https://gist.github.com/e69fd9f9ef85eb3f30a3b93d2cc9b9b3\",\n" +
" \"files\": {\n" +
" \"sample template\": {\n" +
" \"filename\": \"sample template\",\n" +
" \"type\": \"text/plain\",\n" +
" \"language\": null,\n" +
" \"raw_url\": \"https://gist.githubusercontent.com/becauseqa-walter/e69fd9f9ef85eb3f30a3b93d2cc9b9b3/raw/a85b555ce30da1f13aa3b7db3a2756bd64462278/sample%20template\",\n" +
" \"size\": 877\n" +
" }\n" +
" },\n" +
" \"public\": false,\n" +
" \"created_at\": \"2017-05-16T13:28:44Z\",\n" +
" \"updated_at\": \"2017-05-16T13:28:44Z\",\n" +
" \"description\": \"\",\n" +
" \"comments\": 0,\n" +
" \"user\": null,\n" +
" \"comments_url\": \"https://api.github.com/gists/e69fd9f9ef85eb3f30a3b93d2cc9b9b3/comments\",\n" +
" \"owner\": {\n" +
" \"login\": \"becauseqa-walter\",\n" +
" \"id\": 5029046,\n" +
" \"avatar_url\": \"https://avatars1.githubusercontent.com/u/5029046?v=3\",\n" +
" \"gravatar_id\": \"\",\n" +
" \"url\": \"https://api.github.com/users/becauseqa-walter\",\n" +
" \"html_url\": \"https://github.com/becauseqa-walter\",\n" +
" \"followers_url\": \"https://api.github.com/users/becauseqa-walter/followers\",\n" +
" \"following_url\": \"https://api.github.com/users/becauseqa-walter/following{/other_user}\",\n" +
" \"gists_url\": \"https://api.github.com/users/becauseqa-walter/gists{/gist_id}\",\n" +
" \"starred_url\": \"https://api.github.com/users/becauseqa-walter/starred{/owner}{/repo}\",\n" +
" \"subscriptions_url\": \"https://api.github.com/users/becauseqa-walter/subscriptions\",\n" +
" \"organizations_url\": \"https://api.github.com/users/becauseqa-walter/orgs\",\n" +
" \"repos_url\": \"https://api.github.com/users/becauseqa-walter/repos\",\n" +
" \"events_url\": \"https://api.github.com/users/becauseqa-walter/events{/privacy}\",\n" +
" \"received_events_url\": \"https://api.github.com/users/becauseqa-walter/received_events\",\n" +
" \"type\": \"User\",\n" +
" \"site_admin\": false\n" +
" },\n" +
" \"truncated\": false\n" +
" }";
ObjectMapper mapper= new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
GistObject gistObject =mapper.readValue(json, GistObject.class);
그러면 반환 된 GistObject
with 필드 id
는 null
예상되지 않습니다 e69fd9f9ef85eb3f30a3b93d2cc9b9b3
. 그래서 SOmeone은 Java bean 클래스에 모든 json 문자열의 필드를 작성하지 않고 json 문자열을 Java bean으로 deserialize하는 방법을 알고 있습니다. 귀하의 응답에 감사드립니다!
다음과 같이 데이터 클래스에 속성 ID에 대한 setter를 추가해야합니다.
public static class GistObject {
private String id;
public void setId(String id) { this.id = id; }
}
또는 JsonProperty 주석을 사용할 수 있습니다.
public static class GistObject {
@JsonProperty
private String id;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다