Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取资源时出错";无法反序列化实例…”;_Java_Json_Spring Boot_Resttemplate - Fatal编程技术网

Java 获取资源时出错";无法反序列化实例…”;

Java 获取资源时出错";无法反序列化实例…”;,java,json,spring-boot,resttemplate,Java,Json,Spring Boot,Resttemplate,我是java和spring boot新手,正在学习使用RestTemplate使用REST端点。我正在尝试Github jobs api,显然我正在尝试GET/positions/ID.json端点 我创建了以下实体作为响应应该绑定到的类型 package com.example.demo.entity; import java.util.Date; public class GithubJob { private Long id; private String type;

我是java和spring boot新手,正在学习使用RestTemplate使用REST端点。我正在尝试Github jobs api,显然我正在尝试
GET/positions/ID.json
端点

我创建了以下实体作为响应应该绑定到的类型

package com.example.demo.entity;

import java.util.Date;

public class GithubJob {

    private Long id;
    private String type;
    private String url;
    private Date created_at;
    private String company;
    private String company_url;
    private String location;
    private String title;
    private String description;
    private String how_to_apply;
    private String company_log;

    public GithubJob() {
    }

    public GithubJob(
        Long id,
        String type,
        String url,
        Date created_at,
        String company,
        String company_url,
        String location,
        String title,
        String description,
        String how_to_apply,
        String company_log
    ) {
        this.id = id;
        this.type = type;
        this.url = url;
        this.created_at = created_at;
        this.company = company;
        this.company_url = company_url;
        this.location = location;
        this.title = title;
        this.description = description;
        this.how_to_apply = how_to_apply;
        this.company_log = company_log;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Date getCreated_at() {
        return created_at;
    }

    public void setCreated_at(Date created_at) {
        this.created_at = created_at;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getCompany_url() {
        return company_url;
    }

    public void setCompany_url(String company_url) {
        this.company_url = company_url;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getHow_to_apply() {
        return how_to_apply;
    }

    public void setHow_to_apply(String how_to_apply) {
        this.how_to_apply = how_to_apply;
    }

    public String getCompany_log() {
        return company_log;
    }

    public void setCompany_log(String company_log) {
        this.company_log = company_log;
    }

    @Override
    public String toString() {
        return "GithubJob{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", url='" + url + '\'' +
                ", created_at=" + created_at +
                ", company='" + company + '\'' +
                ", company_url='" + company_url + '\'' +
                ", location='" + location + '\'' +
                ", title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", how_to_apply='" + how_to_apply + '\'' +
                ", company_log='" + company_log + '\'' +
                '}';
    }
}
这就是方法

public GithubJob getGithubJobById(String id) {
    RestTemplate restTemplate = new RestTemplate();

    GithubJob githubJob = restTemplate.getForObject("https://jobs.github.com/positions/{id}.json", GithubJob.class, id);

    return githubJob;
}
我收到以下错误消息

Error while extracting response for type [class com.example.demo.entity.GithubJob] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.example.demo.entity.GithubJob` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.example.demo.entity.GithubJob` out of START_ARRAY token at [Source: (PushbackInputStream); line: 1, column: 1]
最后,我要说的是,id是存在的

更新1

这是我期待的回应

更新2 原始错误与我使用长类型作为id有关。使用字符串解决初始消息。现在我得到了这个信息

JSON分析错误:无法反序列化类型
java.util.Date
来自字符串“Wed Jan 20 15:09:48 UTC 2021”


我设法解决了这个问题,首先将id字段的数据类型设置为String,然后将带有值的模式添加到“EEE MMM d hh:mm:ss z yyyy”,这两种情况都是无法解析答案的原因

最终结果如下

package com.example.demo.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

public class GithubJob {

    private String id;
    private String type;
    private String url;
    @JsonFormat(pattern = "EEE MMM d hh:mm:ss z yyyy")
    private Date created_at;
    private String company;
    private String company_url;
    private String location;
    private String title;
    private String description;
    private String how_to_apply;
    private String company_log;

    public GithubJob() {
    }

    public GithubJob(
        String id,
        String type,
        String url,
        Date created_at,
        String company,
        String company_url,
        String location,
        String title,
        String description,
        String how_to_apply,
        String company_log
    ) {
        this.id = id;
        this.type = type;
        this.url = url;
        this.created_at = created_at;
        this.company = company;
        this.company_url = company_url;
        this.location = location;
        this.title = title;
        this.description = description;
        this.how_to_apply = how_to_apply;
        this.company_log = company_log;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Date getCreated_at() {
        return created_at;
    }

    public void setCreated_at(Date created_at) {
        this.created_at = created_at;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getCompany_url() {
        return company_url;
    }

    public void setCompany_url(String company_url) {
        this.company_url = company_url;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getHow_to_apply() {
        return how_to_apply;
    }

    public void setHow_to_apply(String how_to_apply) {
        this.how_to_apply = how_to_apply;
    }

    public String getCompany_log() {
        return company_log;
    }

    public void setCompany_log(String company_log) {
        this.company_log = company_log;
    }

    @Override
    public String toString() {
        return "GithubJob{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", url='" + url + '\'' +
                ", created_at=" + created_at +
                ", company='" + company + '\'' +
                ", company_url='" + company_url + '\'' +
                ", location='" + location + '\'' +
                ", title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", how_to_apply='" + how_to_apply + '\'' +
                ", company_log='" + company_log + '\'' +
                '}';
    }
}

将org.apache.http的日志级别设置为调试并查看响应。它似乎返回一个数组,而不是一个对象。显示您试图反序列化的JSON。没有这些,我们只是猜测。我更新了问题