aims
In SpringBoot interface, we generally use @RequestBody
the class annotation need to deserialize objects, but in the case where there are a plurality of sub-classes, the deserialization routine can not meet the requirements, such as:
We have a piece of paper used to represent the class Exam:
@Data
public class Exam {
private String name;
private List<Question> questions;
}
复制代码
Here Question
special, Question itself is an abstract class that provides some common method invocation, the actual sub-class has multiple choice, multiple choice, true problem many cases
achieve
SprintBoot built serialization is used Jackson, Jackson found after inspection document provided @JsonTypeInfo
and @JsonSubTypes
these two notes, with the use, may be used in an instance of a particular subclass of the specified type field value
These actual class code as follows:
abstract base class Question:
@Data
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "type",
visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = SingleChoiceQuestion.class, name = Question.SINGLE_CHOICE),
@JsonSubTypes.Type(value = MultipleChoiceQuestion.class, name = Question.MULTIPLE_CHOICE),
@JsonSubTypes.Type(value = TrueOrFalseQuestion.class, name = Question.TRUE_OR_FALSE),
})
public abstract class Question {
protected static final String SINGLE_CHOICE = "single_choice";
protected static final String MULTIPLE_CHOICE = "multiple_choice";
protected static final String TRUE_OR_FALSE = "true_or_false";
protected String type;
protected String content;
protected String answer;
protected boolean isCorrect(String answer) {
return this.answer.equals(answer);
}
}
复制代码
True or False TrueOrFalseQuestion:
@Data
@EqualsAndHashCode(callSuper = true)
public class TrueOrFalseQuestion extends Question {
public TrueOrFalseQuestion() {
this.type = TRUE_OR_FALSE;
}
}
复制代码
Choice ChoiceQuestion:
@Data
@EqualsAndHashCode(callSuper = true)
public abstract class ChoiceQuestion extends Question {
private List<Option> options;
@Data
public static class Option {
private String code;
private String content;
}
}
复制代码
Multiple choice SingleChoiceQuestion:
@Data
@EqualsAndHashCode(callSuper = true)
public class SingleChoiceQuestion extends ChoiceQuestion {
public SingleChoiceQuestion() {
this.type = SINGLE_CHOICE;
}
}
复制代码
Multiple choice MultipleChoiceQuestion:
@Data
@EqualsAndHashCode(callSuper = true)
public class MultipleChoiceQuestion extends ChoiceQuestion {
public MultipleChoiceQuestion() {
this.type = MULTIPLE_CHOICE;
}
@Override
public void setAnswer(String answer) {
this.answer = sortString(answer);
}
@Override
public boolean isCorrect(String answer) {
return this.answer.equals(sortString(answer));
}
private String sortString(String str) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
return String.valueOf(chars);
}
}
复制代码
test
The next test
to define an interface, we can use @RequestBody pass a Exam object, and returns the analysis results:
@RequestMapping(value = "/exam", method = RequestMethod.POST)
public List<String> parseExam(@RequestBody Exam exam) {
List<String> results = new ArrayList<>();
results.add(String.format("Parsed an exam, name = %s", exam.getName()));
results.add(String.format("Exam has %s questions", exam.getQuestions().size()))
List<String> types = new ArrayList<>();
for (Question question : exam.getQuestions()) {
types.add(question.getType());
}
results.add(String.format("Questions types: %s", types.toString()));
return results;
}
复制代码
Project up and running, call interface test:
curl -X POST \
http://127.0.0.1:8080/exam/ \
-H 'Content-Type: application/json' \
-d '{
"name":"一场考试",
"questions": [
{
"type": "single_choice",
"content": "单选题",
"options": [
{
"code":"A",
"content": "选项A"
},{
"code":"B",
"content": "选项B"
}],
"answer": "A"
},{
"type": "multiple_choice",
"content": "多选题",
"options": [
{
"code":"A",
"content": "选项A"
},{
"code":"B",
"content": "选项B"
}],
"answer": "AB"
},{
"type": "true_or_false",
"content": "判断题",
"answer": "True"
}]
}'
复制代码
Interface returns the following:
[
"Parsed an exam, name = 一场考试",
"Exam has 3 questions",
"Questions types: [single_choice, multiple_choice, true_or_false]"
]
复制代码
Here the different types of question, type fields can be read correctly, show deserialization process is indeed called concrete subclasses corresponding class is instantiated.