POST上的Spring Rest Json映射(无效属性)

时间:2016-02-18 21:20:16

标签: java json spring hibernate

这对我来说很奇怪。我之前已经完成了实体和控制器以及表单验证,但我对此错误感到困惑。

所以背景故事。这是带有Hibernate的spring-boot,连接到PostgreSQL Db。我正在尝试做的是将POST请求映射到创建资源。我正试图用纯JSON做到这一点。我以前能够实现这一目标。

有问题的错误是......

  

bean类的无效属性'Test'[com.example.api.entities.forms.OrganizationRegistrationForm]:Bean属性'Test'不可读或具有无效的getter方法:getter的返回类型是否与参数匹配setter的类型?

请求正文,就像在Postman中一样......

{
   "organizationName":"Test",
   "employees":10
}

它正在抱怨的 OrganizationRegistrationForm 类......

public class OrganizationRegistrationForm {
    @NotEmpty
    private String organizationName = "";

    @NotNull
    private int employees;

    private JsonNode contactInfo;
    private JsonNode locationInfo;


    public String getOrganizationName() {
        return organizationName;
    }
    public void setOrganizationName(String name) {
        this.organizationName = name;
    }
    public int getEmployees() {
        return employees;
    }
    public void setEmployees(int employees) {
        this.employees = employees;
    }
    public JsonNode getContactInfo() {
        return contactInfo;
    }
    public void setContactInfo(JsonNode contactInfo) {
        this.contactInfo = contactInfo;
    }
    public JsonNode getLocationInfo() {
        return locationInfo;
    }
    public void setLocationInfo(JsonNode locationInfo) {
        this.locationInfo = locationInfo;
    }


}

如果您需要,请求方法......

@RequestMapping(value="/organization", method = RequestMethod.POST)
    public Organization registerOrganization(@Valid @RequestBody OrganizationRegistrationForm form,
            BindingResult bindingResult) throws Exception {
        if(bindingResult.hasErrors()) {
            LOGGER.error("The registration form entered has errors: {}", bindingResult.getAllErrors().toString());
            throw new InvalidForm();
        }

        try {
            Organization org = orgService.registerOrganization(form);
            if(org!=null)
                return org;
        } catch(DataIntegrityViolationException e) {
            bindingResult.reject("name.exists", "The supplied name is already in use");
        }

        throw new InvalidForm();
    }

虽然我猜它甚至没有那么远。最初orginazationName字段被称为“名称”,但我改变它以查看是否可能是问题。

对我来说,更奇怪的部分就是当我使用它的JSON对象时。但创建了一个名为“organizationName”的组织。

{
    "organizationName":"organizationName",
    "employees":10
}

有一次它甚至抱怨无效财产是''。如在空。我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

我不知道如何或为什么。但由于某种原因,答案似乎是在绑定器使用的 OrganizationRegistrationFormValidator 类中。

有问题的恶行在验证(对象目标,错误错误)方法......

 ValidationUtils.rejectIfEmptyOrWhitespace(errors, target.getOrganizationName(), "name.empty", "Please enter a name");

将该行更改为经典检查。

if(target.getOrganizationName.isEmpty())
   errors.reject("name.empty", "Please enter a name");

为了便于记录,任何人都知道为什么会这样?当IntelliSense建议使用方法签名时,我的api文档是错误的吗?

答案 1 :(得分:0)

我知道这已经过时但我偶然发现了它:

对我ValidationUtils.rejectIfEmptyOrWhitespace(errors, target.getOrganizationName(), "name.empty", "Please enter a name");看起来不对。

应该是:

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "organizationName", "name.empty", "Please enter a name");

第二个属性是字段名称,而不是其内容。 ValidationUtils将获取该名称并将其转换为标准getter(在这种情况下为getOrganizationName)以检索其值并验证该值。

这就是为什么它告诉你没有名为Test的属性。因为没有。