почему я получаю JSON ошибку разбора: Невозможно десериализовать экземпляр `* моего объекта * 'из START_OBJECT с помощью restTemplate.getForEntity () и простого поискового запроса - PullRequest
0 голосов
/ 12 января 2020

Я хочу получить всю информацию для данного имени пользователя из API github, я использую весеннюю загрузку 2.2.2

Я не могу проанализировать json возвращение из API, даже если я отобразил Пользователь объекта. здесь Класс, в котором я использую getForEntity: я также пытался передать простой поисковый запрос, но не могу получить json.

Ошибка:

JSON parse ошибка: невозможно десериализовать экземпляр [Lcom.prj.events.User; из токена START_OBJECT; вложенное исключение: com.faster xml .jackson.databind.ex c .MismatchedInputException: Невозможно десериализовать экземпляр [Lcom.prj.resGenerator.events.User; из токена START_OBJECT в [Source: (PushbackInputStream); строка: 1, столбец: 1]

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.prj.resGenerator.events.User;

@Component
public class GithubService {
    private final RestTemplate restTemplate;


    public  GithubService(RestTemplateBuilder builder) {
        this.restTemplate = builder.build();
    }

    public ResponseEntity<User[]> fetchUser(String username) {

        String query = "https://api.github.com/search/users?q=tom";

       //it is the line which contains the error
        return this.restTemplate.getForEntity(query, User[].class);
    }
}

Здесь объект User:

package com.prj.resGenerator.events;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class User {
    private  Long totCount;
    private  Boolean incompleteRes;
    private  Items[] items;


    @JsonCreator
    public User(@JsonProperty("total_count") Long totCount,
            @JsonProperty("incomplete_results") Boolean incompleteRes,
            @JsonProperty("items") Items[] items) {
        super();
        this.totCount = totCount;
        this.incompleteRes = incompleteRes;
        this.items= items;
    }
    public long getTotCount() {
        return totCount;
    }
    public Boolean getIncompleteRes() {
        return incompleteRes;
    }
    public Items[] getItems() {
        return items;
    }

}

Здесь объект Items:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;


public class Items {
private final String login;
private final Long id;
private final String nodeId;
private final String avatarUrl;
private final String gravatarUrl;
private final String url;
private final String htmlUrl;
private final String followersUrl;
private final String followingUrl;
private final String gistsUrl;
private final String starredUrl;
private final String subscriptionsUrl;
private final String organizationsUrl;
private final String reposUrl;
private final String eventsUrl;
private final String recEventUrl;
private final String type;
private final Boolean siteAdmin;
private final Long score;

@JsonCreator
public Items(@JsonProperty("login") String login,
        @JsonProperty("id") Long id, 
        @JsonProperty("node_id") String nodeId,
        @JsonProperty("avatar_url") String avatarUrl,
        @JsonProperty("gravatar_id") String gravatarUrl,
        @JsonProperty("url") String url,
        @JsonProperty("html_url") String htmlUrl,
        @JsonProperty("followers_url") String followersUrl, 
        @JsonProperty("following_url") String followingUrl, 
        @JsonProperty("gists_url") String gistsUrl, 
        @JsonProperty("starred_url") String starredUrl, 
        @JsonProperty("subscriptions_url") String subscriptionsUrl,
        @JsonProperty("organizations_url") String organizationsUrl,
        @JsonProperty("repos_url") String reposUrl, 
        @JsonProperty("events_url") String eventsUrl,
        @JsonProperty("received_events_url") String recEventUrl,
        @JsonProperty("type") String type,
        @JsonProperty("site_admin") Boolean siteAdmin,
        @JsonProperty("score") Long score) {
    super();
    this.login = login;
    this.id = id;
    this.nodeId = nodeId;
    this.avatarUrl = avatarUrl;
    this.gravatarUrl = gravatarUrl;
    this.url = url;
    this.htmlUrl = htmlUrl;
    this.followersUrl = followersUrl;
    this.followingUrl = followingUrl;
    this.gistsUrl = gistsUrl;
    this.starredUrl = starredUrl;
    this.subscriptionsUrl = subscriptionsUrl;
    this.organizationsUrl = organizationsUrl;
    this.reposUrl = reposUrl;
    this.eventsUrl = eventsUrl;
    this.recEventUrl = recEventUrl;
    this.type = type;
    this.siteAdmin = siteAdmin;
    this.score = score;
}

//getters

}

Это json, которое я получаю от API, если пытаюсь вызвать его из командной строки (curl):

{
  "total_count": 6,
  "incomplete_results": false,
  "items": [
    {
      "login": "mojombo",
      "id": 1,
      "node_id": "MDQ6VXNlcjE=",
      "avatar_url": "https://avatars0.githubusercontent.com/u/1?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/mojombo",
      "html_url": "https://github.com/mojombo",
      "followers_url": "https://api.github.com/users/mojombo/followers",
      "following_url": "https://api.github.com/users/mojombo/following{/other_user}",
      "gists_url": "https://api.github.com/users/mojombo/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/mojombo/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/mojombo/subscriptions",
      "organizations_url": "https://api.github.com/users/mojombo/orgs",
      "repos_url": "https://api.github.com/users/mojombo/repos",
      "events_url": "https://api.github.com/users/mojombo/events{/privacy}",
      "received_events_url": "https://api.github.com/users/mojombo/received_events",
      "type": "User",
      "site_admin": false,
      "score": 97.372185
    },
    {
      "login": "tmcw",
      "id": 32314,
      "node_id": "MDQ6VXNlcjMyMzE0",
      "avatar_url": "https://avatars2.githubusercontent.com/u/32314?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/tmcw",
      "html_url": "https://github.com/tmcw",
      "followers_url": "https://api.github.com/users/tmcw/followers",
      "following_url": "https://api.github.com/users/tmcw/following{/other_user}",
      "gists_url": "https://api.github.com/users/tmcw/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/tmcw/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/tmcw/subscriptions",
      "organizations_url": "https://api.github.com/users/tmcw/orgs",
      "repos_url": "https://api.github.com/users/tmcw/repos",
      "events_url": "https://api.github.com/users/tmcw/events{/privacy}",
      "received_events_url": "https://api.github.com/users/tmcw/received_events",
      "type": "User",
      "site_admin": false,
      "score": 84.700485
    },
    {
      "login": "tomnomnom",
      "id": 58276,
      "node_id": "MDQ6VXNlcjU4Mjc2",
      "avatar_url": "https://avatars1.githubusercontent.com/u/58276?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/tomnomnom",
      "html_url": "https://github.com/tomnomnom",
      "followers_url": "https://api.github.com/users/tomnomnom/followers",
      "following_url": "https://api.github.com/users/tomnomnom/following{/other_user}",
      "gists_url": "https://api.github.com/users/tomnomnom/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/tomnomnom/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/tomnomnom/subscriptions",
      "organizations_url": "https://api.github.com/users/tomnomnom/orgs",
      "repos_url": "https://api.github.com/users/tomnomnom/repos",
      "events_url": "https://api.github.com/users/tomnomnom/events{/privacy}",
      "received_events_url": "https://api.github.com/users/tomnomnom/received_events",
      "type": "User",
      "site_admin": false,
      "score": 74.80121
    },
    {
      "login": "tomdale",
      "id": 90888,
      "node_id": "MDQ6VXNlcjkwODg4",
      "avatar_url": "https://avatars2.githubusercontent.com/u/90888?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/tomdale",
      "html_url": "https://github.com/tomdale",
      "followers_url": "https://api.github.com/users/tomdale/followers",
      "following_url": "https://api.github.com/users/tomdale/following{/other_user}",
      "gists_url": "https://api.github.com/users/tomdale/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/tomdale/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/tomdale/subscriptions",
      "organizations_url": "https://api.github.com/users/tomdale/orgs",
      "repos_url": "https://api.github.com/users/tomdale/repos",
      "events_url": "https://api.github.com/users/tomdale/events{/privacy}",
      "received_events_url": "https://api.github.com/users/tomdale/received_events",
      "type": "User",
      "site_admin": false,
      "score": 73.56921
    },
    {
      "login": "tommcfarlin",
      "id": 132166,
      "node_id": "MDQ6VXNlcjEzMjE2Ng==",
      "avatar_url": "https://avatars0.githubusercontent.com/u/132166?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/tommcfarlin",
      "html_url": "https://github.com/tommcfarlin",
      "followers_url": "https://api.github.com/users/tommcfarlin/followers",
      "following_url": "https://api.github.com/users/tommcfarlin/following{/other_user}",
      "gists_url": "https://api.github.com/users/tommcfarlin/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/tommcfarlin/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/tommcfarlin/subscriptions",
      "organizations_url": "https://api.github.com/users/tommcfarlin/orgs",
      "repos_url": "https://api.github.com/users/tommcfarlin/repos",
      "events_url": "https://api.github.com/users/tommcfarlin/events{/privacy}",
      "received_events_url": "https://api.github.com/users/tommcfarlin/received_events",
      "type": "User",
      "site_admin": false,
      "score": 73.20562
    },
    {
      "login": "tommy351",
      "id": 411425,
      "node_id": "MDQ6VXNlcjQxMTQyNQ==",
      "avatar_url": "https://avatars0.githubusercontent.com/u/411425?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/tommy351",
      "html_url": "https://github.com/tommy351",
      "followers_url": "https://api.github.com/users/tommy351/followers",
      "following_url": "https://api.github.com/users/tommy351/following{/other_user}",
      "gists_url": "https://api.github.com/users/tommy351/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/tommy351/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/tommy351/subscriptions",
      "organizations_url": "https://api.github.com/users/tommy351/orgs",
      "repos_url": "https://api.github.com/users/tommy351/repos",
      "events_url": "https://api.github.com/users/tommy351/events{/privacy}",
      "received_events_url": "https://api.github.com/users/tommy351/received_events",
      "type": "User",
      "site_admin": false,
      "score": 72.454994
    }
  ]
}

Я новичок в API и не могу получить к точке. большое спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...