user3301081
user3301081

Reputation:

Spring XML - no String-argument constructor/factory method to deserialize from String value

I'm getting this error trying to de-serialize some XML from a Spring RestController.

10:32:20.275 [main] WARN  org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.example.SomeThing: no String-argument constructor/factory method to deserialize from String value ('AAAA')

Here is the class (changed names and packages)

public final class SomeThing{
    public static final SomeThing AAAA = create("AAAA");

    public static SomeThing create(final String value) {
      SomeThing result = new SomeThing ();
      result.setValue(value);
      return result;
    }
}

So how do alter this class so it is able to be de-serialized?

Upvotes: 0

Views: 203

Answers (1)

tmarwen
tmarwen

Reputation: 16374

You should mark the Something#create method as the factory method so it's resolved as the method for instantiating new Something instances.

Here is a modified version of your class (note that it has been altered to match the declared fields in the main OP):

public final class SomeThing{

  private String val;

  @JsonCreator
  public static SomeThing create(final String value) {
    SomeThing result = new SomeThing();
    result.setValue(value);
    return result;
  }

  public void setValue(String value) {
    this.val = value;
  }
}

Upvotes: 1

Related Questions