Failed To Convert Property Value Of Type 'java.lang.String' - Receive Object With Another Object Inside Spring

- 1 answer

I'm trying to receive from my controller in Spring a Object with another object inside of it, but I got this error:

I'm doing the call with:

type: "POST",
headers: {
    Authorization: $cookieStore.get("userPassword"),
    FeatureName: name,
success: function (data) {
    console.log("reload page");

dataType: "text"

I get the error:

Failed to convert property value of type 'java.lang.String' to required type 'com.Option' for property 'option'

this is the whole output line:

org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'modelDTO' on field 'option': rejected value [{"id":3,"optionName":"Other"}]; codes [typeMismatch.modelDTO.option,typeMismatch.option,,typeMismatch]; arguments [ codes [modelDTO.option,option]; arguments []; default message [option]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.Option' for property 'option'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.Option] for property 'option': no matching editors or conversion strategy found]

this is my controller and the DTO

@RequestMapping(value = "/save", method = RequestMethod.POST)
public @ResponseBody void save(@ModelAttribute("form") ModelDTO model)

public class ModelDTO implements Serializable 

    private static final long serialVersionUID = 3276874897891652914L;
    private Long userId;
    private Option option;

    public Long getUserId()
        return userId;

    public void setUserId(Long userId)
        this.userId = userId;

    public Option getOption()
        return option;

    public void setOption(Option option)
        this.option = option;


Does spring allow do this work?



I got the solution, I don't need change anything of my code only add initBinderAll in the controller where I'm doing the binding. With this solution we can handle the data which are not converting automatically.


    public void initBinderAll(WebDataBinder binder) 
        binder.registerCustomEditor(Option.class,  new OptionPropertyEditor());

and create the custom PropertyEditorSupport like:

public class OptionPropertyEditor extends PropertyEditorSupport