4

I want to know if there is a way to parse json data dynamically into different object by using jackson feature in Spring3.

I have a parent class as below:

public class Recording {
    private String id;

    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }
}

And two children:

public class Child1Recording extends Recording {
    private String program;

    public String getProgram() {
        return program;
    }

    public void setProgram(String program) {
        this.program = program;
    }
}

public class Child2Recording extends Recording {
    private String time;

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

Controller like this:

@RequestMapping(value = "/init/postCheck.ajax", method = RequestMethod.POST)
public @ResponseBody
String postCheck(@RequestBody Recording recording) {
    if (recording instanceof Child2Recording) {
        return "\"child2 success\"";
    } else if (recording instanceof Child1ecording) {
        return "\"child1 success\"";
    }
    return "\"only parent Recording\"";
}

i have different scenarios to post different json data to the backend, i am wondering if there is a way to make controller works like i said above?

For now, if i send a Child2Recording data, an error occurs when parsing it. I can't get the correct object that i expect.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.