org.codehaus.jackson.map.JsonMappingException:无法从 JSON 字符串实例化 [简单类型,类models.Job] 类型的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11127376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:24:58  来源:igfitidea点击:

org.codehaus.Hymanson.map.JsonMappingException: Can not instantiate value of type [simple type, class models.Job] from JSON String

jsonrestplayframeworkHymanson

提问by Markus Pleines

i use the playframework and tried to deserialize some json into a java object. It worked fine, exept the relationship in the model. I got the following exception

我使用 playframework 并尝试将一些 json 反序列化为 java 对象。它工作正常,除了模型中的关系。我得到以下异常

enter code hereorg.codehaus.Hymanson.map.JsonMappingException: Can not instantiate value of type [simple type, class models.Job] from JSON String; no single-String constructor/factory method (through reference chain: models.Docfile["job"])

在此处输入代码org.codehaus.Hymanson.map.JsonMappingException:无法从JSON字符串实例化[简单类型,类models.Job]类型的值;没有单字符串构造函数/工厂方法(通过参考链:models.Docfile["job"])

i thought Hymanson in combination with play could do that:

我认为 Hymanson 与 play 相结合可以做到这一点:

this is the json

这是json

{"name":"asd","filepath":"blob","contenttype":"image/png","description":"asd","job":"1"}

and this my code, nothing special:

这是我的代码,没什么特别的:

public static Result getdata(String dataname) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            Docfile docfile = mapper.readValue((dataname), Docfile.class);
            System.out.println(docfile.name);
            docfile.save();

        } catch (JsonGenerationException e) {

            e.printStackTrace();

        } catch (JsonMappingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return ok();
    }

Hope there is help for me, thanks Markus

希望对我有帮助,谢谢马库斯

UPDATE:

更新:

Docfile Bean:

文档豆:

package models;

import java.util.*;

import play.db.jpa.*;
import java.lang.Object.*;
import play.data.format.*;
import play.db.ebean.*;
import play.db.ebean.Model.Finder;
import play.data.validation.Constraints.*;
import play.data.validation.Constraints.Validator.*;

import javax.persistence.*;

import com.avaje.ebean.Page;

@Entity
public class Docfile extends Model {

    @Id
    public Long id;

    @Required
    public String name;

    @Required
    public String description;

    public String filepath;

    public String contenttype;

    @ManyToOne
    public Job job;

    public static Finder<Long,Docfile> find = new Model.Finder(
            Long.class, Docfile.class
            );




    public static List<Docfile> findbyJob(Long job) {
        return find.where()
                .eq("job.id", job)
                .findList();
    }

    public static Docfile create (Docfile docfile, Long jobid) {
        System.out.println(docfile);
        docfile.job = Job.find.ref(jobid);
        docfile.save();
        return docfile;
    }
}

采纳答案by ndeverge

Either you change your JSON in order to describe your "job" entity :

您要么更改 JSON 以描述您的“工作”实体:

{
   "name":"asd",
   "filepath":"blob",
   "contenttype":"image/png",
   "description":"asd",
   "job":{
      "id":"1",
       "foo", "bar"
   }
}

or you create a constructor with a String parameter in your Job bean:

或者您在 Job bean 中创建一个带有 String 参数的构造函数:

public Job(String id) {
// populate your job with its id
}

回答by Oleksii Kyslytsyn

when limited time +ee: +jax-rs && +persistence, +gson; I have solved it then as:

时间有限时 +ee: +jax-rs && +persistence, +gson; 我已经解决了它:

@Entity
@XmlRootElement
@Table(name="element")
public class Element implements Serializable {
    public Element(String stringJSON){
        Gson g = new Gson();
        Element a = g.fromJson(stringJSON, this.getClass());
        this.setId(a.getId());
        this.setProperty(a.getProperty());
    }

    public Element() {}
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    ...
}