0

I have ArrayList child like this:

public class SomeList<T> extends ArrayList<T> {
    private Class<T> clazz;

    public SomeList(Class<T> clazz) {
        this.clazz = clazz;
    }

    public boolean add(T t) {
        return t != null ? super.add(t) : false;
    }

    public Class getGenericClass() {
        return this.clazz;
    }
}

I have no option but to use it (backward compatibility), i can't modify it and I need to deserialize it in spring controller, e.g.:

@RequestMapping(value = "/somethingHappens", method = {RequestMethod.POST})
@ResponseBody
public ServiceResult somethingHappens(@RequestBody SomeList<SomeObject> someObjects) {
    return ResponseUtils.createOkJsonResponse(someService.getSomethingElse(someObjects));
}

I don't need to have clazz nor received, nor deserialized, I just want to deserialize this SomeList the same way as it was ArrayList, but it is not possible at this moment because it has no default constructor.

Can I use MixIns somehow? Can I use custom deserializer and somehow tell him "just do it same way as ArrayList"?

stove
  • 556
  • 6
  • 24
  • Possible solutions here https://stackoverflow.com/questions/11664894/jackson-deserialize-using-generic-class – Kayaman Nov 29 '17 at 08:28

1 Answers1

0

It's unclear how much editing you can do in your Spring controller, but one potential solution is to create another class with a default constructor, which extends the troublesome class, and use that in the controller instead:

public class ExtSomeList extends SomeList<SomeType> {
    public ExtSomeList() {
        super(SomeType.class);
    }
}

Now use that inside the controller instead:

public ServiceResult somethingHappens(@RequestBody ExtSomeList someObjects) {
    return ResponseUtils.createOkJsonResponse(someService.getSomethingElse(someObjects));
}
Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
  • If it was only about controller parameters I would use ArrayList no problem, but I have a big model in which this list is used, cannot be substituted/changed and needs to be deserialized. – stove Dec 04 '17 at 08:18