json - JSON字符串中的org.codehaus.jackson.map.JsonMappingException : Can not instantiate value of type [simple type,类模型。

标签 json rest playframework jackson

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

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 结合游戏可以做到这一点:

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

最佳答案

您可以更改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
}

关于json - JSON字符串中的org.codehaus.jackson.map.JsonMappingException : Can not instantiate value of type [simple type,类模型。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11127376/

相关文章:

python - 如何使用 google docs api (Python) 更新 google doc

javascript - jquery 和 json 数组

java - 休息 jax-rs : server error

php - Rest Api 的服务器端实现

scala - 创建通用更新计数器方法

database - 在 Play 中使用进化创建数据库、用户和分配权限是一种不好的做法吗?

sql - 如何通过CROSS APPLY获取多级嵌套JSON属性的值?

android - 将 JSON 发送到服务器

java - 如何获取两个 REST API 响应的差异?

playframework - Play 框架 [2.2-scala] : creating Enumerator from a slow InputStream