我正在尝试将KendoUI网格发送的过滤器参数解析到我的Web服务,并且有一些问题说服Jackson解析这个
JSON.据我所知,我可以控制剑道发送的参数格式,但我不知道如何将参数编组成更好的格式,以便它们现在保持不变.
我打算将这些参数转换为Oracle数据库的SQL查询.
示例JSON:
{
"filters":
[
{
"field": "Name",
"operator": "contains",
"value": "John"
},
{
"filters": [
{
"field": "Age",
"operator": "gt",
"value": 20
},
{
"field": "Age",
"operator": "lt",
"value": 85
}
],
"logic", "and"
},
{
"field": "Address",
"operator": "doesnotcontain",
"value": "street"
}
],
"logic": "or"
}
过滤器. Java的
public class Filters {
private List<Filter> filters;
private String logic;
// accessors/mutators/toString
}
Filter.java
public class Filter {
private String field;
private String operator;
private String value;
// accessors/mutators/toString
}
单元测试
public class KendoGridFilterTest {
private ObjectMapper mapper;
@Before
public void before() {
mapper = new ObjectMapper();
}
@Test
public void jsonParseTest() {
final String json = "{\"filters\":[{\"field\":\"Name\",\"operator\":\"contains\",\"value\":\"John\"},{filters: [{\"field\":\"Age\",\"operator\": \"eq\",\"value\": 85},{\"field\": \"Age\",\"operator\": \"eq\",\"value\": 85}]\"logic\", \"and\",},{\"field\": \"Address\",\"operator\": \"doesnotcontain\",\"value\": \"street\"}],\"logic\":\"or\"}";
Filters filters = mapper.readValue(json, Filters.class);
assertTrue(json.equals(filters.writeValueAsString(filters);
}
}
错误
com.fasterxml.jackson.databind.UnrecognizedPropertyException: Unrecognized field 'logic'(com.example.Filter) not market as ignorable (3 known properties "value", "field", "operator") at [Source: java.io.StringReader@3bb2b8; line: 1, column 76] (through reference chain: com.example.Filters["filters"]->com.example.Filter["logic"]
我也尝试将@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class,property =“@ id”)添加到Filters类并获得相同的错误.
最佳答案 您的Filter类不正确.它应该扩展过滤器.
在更正单元测试(json不正确)后,它可以将您的json加载到Filters对象中.
public class Filter extends Filters {
private String field;
private String operator;
private String value;
// accessors/mutators/toString
}