我在请求反序列化时遇到问题,该请求应该转换为一个 java 类,其中的属性本身就是其他类。第一级反序列化有效,但是对于包含的字段,由于显而易见的原因,我没有获得相应的子类。
这是我的代码:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CustomForm extends GenericJson {
private String formId;
private CustomInfo info;
private List<CustomItem> items = new ArrayList<>();
private String linkedSheetId;
private String responderUri;
private String revisionId;
private FormSettings settings;
public CustomForm(Form form){
BeanUtils.copyProperties(form, this);
if(form.getInfo() != null){
this.info = new CustomInfo(form.getInfo());
}
if(form.getItems() != null){
for(Item item:form.getItems()){
this.items.add(new CustomItem(item));
}
}
}
public Form getForm(){
Form form = new Form();
BeanUtils.copyProperties(this, form);
List<Item> items = new ArrayList<>();
for(CustomItem item:this.items){
items.add(item.getItem());
}
form.setItems(items);
if(!this.info.isEmpty()) {
form.setInfo(this.info.getInfo());
}
return form;
}
}
这是我的要求:
{
"formId": "1234",
"info": {
"documentTitle": "Untitled form",
"title": "prova"
},
"items": [
{
"itemId": "123",
"questionItem": {
"question": {
"choiceQuestion": {
"options": [
{
"value": "Newsletter"
},
{
"value": "Magazine"
},
{
"value": "Social Network"
},
{
"value": "Siti"
},
{
"value": "Associazioni di categoria"
},
{
"value": "Aziende partner"
},
{
"value": "Comunicazione interna all\u0027azienda"
}
],
"type": "DROP_DOWN"
},
"questionId": "123"
}
},
"title": "Come sei venuto a conoscenza di questo Corso?"
},
{
"itemId": "123",
"questionGroupItem": {
"grid": {
"columns": {
"options": [
{
"value": "PESSIMO"
},
{
"value": "MEDIOCRE"
},
{
"value": "SUFFICIENTE"
},
{
"value": "BUONO"
},
{
"value": "OTTIMO"
}
],
"type": "RADIO"
}
},
"questions": [
{
"questionId": "123",
"rowQuestion": {
"title": "Pulizia delle aule"
}
},
{
"questionId": "123",
"rowQuestion": {
"title": "Materiale fornito"
}
},
{
"questionId": "123",
"rowQuestion": {
"title": "Competenza docente"
}
},
{
"questionId": "123",
"rowQuestion": {
"title": "Completezza contenuti"
}
},
{
"questionId": "123",
"rowQuestion": {
"title": "Applicabilità delle nozioni"
}
},
{
"questionId": "123",
"rowQuestion": {
"title": "Rispondenza ad aspettative"
}
},
{
"questionId": "123",
"rowQuestion": {
"title": "Gentilezza del personale"
}
}
]
}
}
],
"responderUri": "https://example.om",
"revisionId": "00000005"
}
问题是:是否有任何注释可以让我将请求字段与其各自的嵌套类相关联?
解决了。
这个构造函数可以解决问题。
@JsonCreator
public CustomForm(
@JsonProperty("formid") String formId,
@JsonProperty("info") CustomInfo info,
@JsonProperty("items") List<CustomItem> items,
@JsonProperty("linkedSheetId") String linkedSheetId,
@JsonProperty("responderUri") String responderUri,
@JsonProperty("revisionId") String revisionId,
@JsonProperty("settings") FormSettings settings
){
this.formId = formId;
this.info = info;
this.items = items;
this.linkedSheetId = linkedSheetId;
this.responderUri = responderUri;
this.revisionId = revisionId;
this.settings = settings;
}