【问题标题】:org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class models.Job] from JSON Stringorg.codehaus.jackson.map.JsonMappingException:无法从 JSON 字符串实例化类型 [简单类型,类 models.Job] 的值
【发布时间】:2012-06-23 01:13:45
【问题描述】:

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

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

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

这是json

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

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

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();
    }

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

更新:

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

【问题讨论】:

  • 请添加Docfile bean的源代码。
  • 更新了 docfile bean

标签: json rest playframework jackson


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    限时+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;
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-25
      • 1970-01-01
      • 2016-10-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      相关资源
      最近更新 更多