我不知道该怎么办这个问题。 我有一个JSON对象来帮助通过POST(使用Play Framework)进行值输入,结构如下:
{
"start_absolute": 1403185486254,
"end_absolute": 1403185486254,
"metrics": [
{
"name": "parts",
"tags": [
{
"key":"machine",
"value":"10"
}
],
"sampling":
{
"value": 1,
"unit": "minutes"
}
}
]
}
在服务器端,我尝试处理这样的信息:
Form<QueryForm> queryForm = Form.form(QueryForm.class).bindFromRequest();
if (queryForm.hasErrors()) {
return badRequest(queryForm.errorsAsJson());
}
QueryForm queryInfo = queryForm.get();
用于在java中定义JSON对象的类
public class QueryForm {
private Date start_absolute;
private Date end_absolute;
private List<MetricForm> metrics= Lists.newArrayList();
public Date getStart_absolute() {
return start_absolute;
}
public void setStart_absolute(long start_absolute) {
this.start_absolute = new Date(start_absolute);
}
public Date getEnd_absolute() {
return end_absolute;
}
public void setEnd_absolute(long end_absolute) {
this.end_absolute =new Date(end_absolute);
}
public List<MetricForm> getMetrics() {
return metrics;
}
public void setMetrics(List<MetricForm> metrics) {
this.metrics = metrics;
}
@Override
public String toString() {
return "QueryForm[start_absolute="+start_absolute+", end_absolute="+end_absolute+", metrics="+metrics+"]";
}
public class MetricForm {
private String name;
private List<Tag> tags= Lists.newArrayList();
private RelativeTimeForm sampling= new RelativeTimeForm(1,"milliseconds");
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags.addAll(tags);
}
public RelativeTimeForm getSampling() {
return sampling;
}
public void setSampling(int val, String unit) {
this.sampling.setUnit(unit);
this.sampling.setVal(val);
}
@Override
public String toString() {
return "MetricForm[name="+name+", tags="+tags+",sampling="+sampling+"]";
}
public class Tag {
private String key;
private String value;
public Tag(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return String.format(
"Tag[key=%s, value='%s']",
key, value
);
}
}
public class RelativeTimeForm {
private int value;
private String unit;
public RelativeTimeForm (int val, String unit){
this.setValue(val);
this.setUnit(unit);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
@Override
public String toString() {
return "RelativeTimeForm[value="+value+", unit="+unit+"]";
}
我没有经验使用play.data.Form,但在以前的更简单的对象(使用原始数据类型和字符串)这个工作正常,但当我尝试POST到播放应用程序(标题:内容类型= application / json; charset = utf-8)它发生内部服务器错误:
play.api.Application$$anon$1: Execution exception [[InvalidPropertyException:
Invalid property 'metrics[0].tags[0]' of bean class [model.QueryForm]: Illegal
attempt to get property 'tags' threw exception; nested exception is
org.springframework.beans.NullValueInNestedPathException: Invalid property
'metrics[0].tags' of bean class [model.QueryForm]: Could not instatiate property
path:
java.lang:InstatiationException: [model.Tag]
任何人都可以帮忙吗?我不知道该怎么做。是从JSON转换为对象的复合体吗?我是否需要添加一些JSON注释(目前尚未使用它们)?
我不知道如何解决这个问题
更新:我将其缩小到Tag类。由于某种原因,我无法通过JSON实现Tag。我甚至尝试在与日期相同的级别设置单个Tag实例,并且它给出了相同的错误(它适用于采样)
解决:
Spring binding exception when a form is submitted
答案在上面的帖子中。我必须在Tag类中设置一个空的construtor才能工作。
public Tag(){
}
答案 0 :(得分:0)
异常意味着没有实例化一个或多个属性。
private List<MetricForm> metrics = new ...;
private List<Tag> tags = new ...;
private RelativeTimeForm sampling = new ...;
应该这样做。
如果您在阅读时间值时遇到问题:我不会直接使用Date
类型来读入,而是使用long
读取时间值然后(内部)转换它们到Date
个对象。