gpt4 book ai didi

java - Play 自定义对象值的框架表单绑定(bind)映射

转载 作者:行者123 更新时间:2023-11-30 07:15:40 24 4
gpt4 key购买 nike

我目前正在尝试使用 Play 中的 Form.form(class).bindfromRequest() 方法绑定(bind)数据库对象的 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()。谁能详细说明为什么这不适用于自定义类? map 工作正常...

最佳答案

你可以尝试这样的事情:

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

关于java - Play 自定义对象值的框架表单绑定(bind)映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com