播放框架表单绑定自定义对象值的地图

时间:2016-07-18 17:56:37

标签: java json playframework

我目前正在尝试使用Play中的Form.form(class).bindfromRequest()方法绑定数据库对象的JSON表示。

我的父类是:SolrSearchQuery

public class SolrSearchQuery {

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "solrQuery")
    @MapKey(name = "name")
    @JsonManagedReference
    @Valid
    protected Map<String, SolrSearchQueryValue> values;

    //standard getter and setter for values
}

我缩写了这个类,因为它非常大,只有这个成员才会导致问题。

SolrSearchQueryValue类如下所示:

@Entity
@Table(name = "solr_search_query_values")
public class SolrSearchQueryValue extends BaseModel {

public static Finder<Long, SolrSearchQueryValue> find = new Finder<Long, SolrSearchQueryValue>(Long.class, SolrSearchQueryValue.class);

@Id
protected Long id;

protected String name;

protected String value;

@ManyToOne
@JoinColumn(name = "solr_search_query_id")
@JsonBackReference
protected SolrSearchQuery solrQuery;

@JsonCreator
protected SolrSearchQueryValue(@JsonProperty("id") Long id,
                               @JsonProperty("name") String name,
                               @JsonProperty("value") String value) {
    this.id = id;
    this.name = name;
    this.value = value;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public SolrSearchQuery getSolrQuery() {
    return solrQuery;
}

public void setSolrQuery(SolrSearchQuery solrQuery) {
    this.solrQuery = solrQuery;
}
}

当我使用以下JSON调用Form.form.bindFromRequest()时:

{
  "id": 1,
  "priority": 0,
  "dataSourceField": [
    {
      "id": 1,
      "searchAreaName": "Automatching",
      "className": "models.Project",
      "fieldName": "",
      "solrClassName": "ws.solr.profiles.SolrProfile",
      "solrFieldName": "locationCoords",
      "alias": "Umkreissuche",
      "queryType": "GEO_LOCATION",
      "hasDataProvider": false,
      "forceManual": true
    }
  ],
  "description": "asd",
  "inputFieldType": "TEXT",
  "queryType": "GEO_LOCATION",
  "detail": true,
  "active": true,
  "boost": 0,
  "options": [],
  "values": {
    "qwe": {
      "id": 9,
      "name": "qwe",
      "value": "qwe"
    }
  },
  "operator": "AND",
  "subQueries": []
}

值不受Play限制。它甚至不会抛出Form.hasErrors()。任何人都可以详细说明为什么这不适用于自定义类?地图工作得很好......

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

@BodyParser.Of(BodyParser.Json.class)
public Result test() {
    JsonNode json = request().body().asJson();
    Model model = Json.fromJson(json, Model.class);
    return ok();
}