问题描述
我使用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
i认为杰克逊与游戏结合可以做到这一点:
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();
}
希望对我有帮助,谢谢
Markus
Hope there is help for me, thanksMarkus
更新:
Docfile Bean:
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来描述你的工作实体:
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字符串实例化类型[simple type,class models.Job]的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!