设置和映射的播放Java验证失败

时间:2017-04-03 14:58:23

标签: java validation playframework-2.0

我使用Play 2.5.12提供Web服务来创建对象并验证其属性。这是我想要做的简化代码:

public class Example {

    private String lastName;

    private List<String> firstNames;

    private Map<String, Integer> vehicles;

    public Example() {}

    public String validate() {
        if (vehicles.get("Ferrari") != null)
            return "Liar!";
        return null;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append(lastName).append(firstNames).append(vehicles).toString();
    }

    // getters and setters
}


public class ExampleController extends Controller {

    private FormFactory formFactory;

    @Inject
    public ExampleController(FormFactory formFactory) {
        this.formFactory = formFactory;
    }

    @BodyParser.Of(BodyParser.Json.class)
    public Result createExample() {
        Example exampleFromJackson = Json.fromJson(request().body().asJson(), Example.class);
        System.out.println(exampleFromJackson.toString());
        Example exampleFromForm = formFactory.form(Example.class).bindFromRequest().get();
        System.out.println(exampleFromForm.toString());
        // etc
        return created();
    }
}

如果我用这样的身体来呼叫网络服务:

{
    "lastName": "Martin",
    "firstNames": ["Robert", "Cecil"],
    "vehicles": {
        "BMW": 1,
        "Seat": 1
    }
}

杰克逊的反序列化对象打印正确,然后发生此错误:

org.springframework.beans.InvalidPropertyException: Invalid property 'firstNames[0]' of bean class [models.Example]: Property referenced in indexed property path 'firstNames[0]' is neither an array nor a List nor a Map; returned value was [[]]

如果我用List替换Set,我可以避免这个错误,然后我得到以下内容:

models.Example@68d2162a[Martin,[Robert, Cecil],{BMW=1, Seat=1}]
models.Example@29ef1c11[Martin,[Robert, Cecil],{}]

杰克逊的反序列化对象是正确的,但请求获得的对象是不正确的,所以我无法进行正确的验证。

所以我的问题是:
- 为什么Play不允许我使用Set?
- 为什么没有检索到地图?

0 个答案:

没有答案