Search code examples
javaspring

How to enforce Spring to convert blank String request parameter to bean property as null, avoiding attempts to construct object?


There is @Controller class with properly configured @RequestMapping(...) method in it.

The method takes Request params into a POJO of type User:

@RequestMapping("")
@ResponseBody
public User edit(User params){...}

where User itself has a field of POJO type 'OrganisationMember':

public class User {

    protected OrganisationMember organisationMemberExtension;

    public OrganisationMember getOrganisationMemberExtension(){
        return this.organisationMemberExtension;
    }
    public void setOrganisationMemberExtension(OrganisationMember organisationMemberExtension){
        this.organisationMemberExtension = organisationMemberExtension;
    }
}

when request has parameter organisationMemberExtension = "", Spring fails with error message:

Field error in object 'user' on field 'organisationMemberExtension': rejected value []; codes [typeMismatch.user.organisationMemberExtension,typeMismatch.organisationMemberExtension,typeMismatch.cool.rs.mrp.data.model.org.OrganisationMember,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.organisationMemberExtension,organisationMemberExtension]; arguments []; default message [organisationMemberExtension]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'cool.rs.mrp.data.model.org.OrganisationMember' for property 'organisationMemberExtension'; Cannot convert value of type 'java.lang.String' to required type 'cool.rs.mrp.data.model.org.OrganisationMember' for property 'organisationMemberExtension': no matching editors or conversion strategy found

So, how to enforce Spring to not even try to convert blank String to some object type, but to set corresponding POJO property to null instead?

Would be great if it is done through some built-in converter implementation and config options, if any. Using @InitBinder method of @Controller class with custom editor:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(OrganisationMember.class, new StringToOrganisationMemberConverter());
}

... is also possible, but less preferred.

In addition, is there some built-in implementation in Spring, able to construct OrganisationMember bean for User.organisationMemberExtension and populate it's fields with organisationMemberExtension.id = 0; organisationMemberExtension.name = "ojdljzd"; ... request parameters?


Solution

  • Firstly, you do not need to send object="". Instead, you should send object.field as a request parameter, as shown below:

    enter image description here

    If you send the object directly, you will encounter an error, as I have experienced. Please see the screenshot below for reference:

    enter image description here

    On eliminating the object, you will have null in that object as shown in below picture.

    enter image description here