org.codehaus.jackson.map.JsonMappingException:无法从 JSON 字符串实例化 [简单类型,类模型.Job] 类型的值 [英] org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class models.Job] from JSON String

查看:32
本文介绍了org.codehaus.jackson.map.JsonMappingException:无法从 JSON 字符串实例化 [简单类型,类模型.Job] 类型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在此处输入代码org.codehaus.jackson.map.JsonMappingException:不能从 JSON 实例化 [simple type, class models.Job] 类型的值细绳;没有单字符串构造函数/工厂方法(通过引用链:models.Docfile["job"])

enter code hereorg.codehaus.jackson.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"])

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

i thought jackson in combination with play could do that:

这是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

更新:

文档 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;
    }
}

推荐答案

要么你改变你的 JSON 来描述你的工作"实体:

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

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

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

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

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

这篇关于org.codehaus.jackson.map.JsonMappingException:无法从 JSON 字符串实例化 [简单类型,类模型.Job] 类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆