0
私は私のDTOを生成するためにswaggerを使用しています。 私はFieldクラスを継承するInputFieldとTextareaField DTOを持っています。Swagger Spring多形DTO
私のYAMLは次の定義があります。
definitions:
field:
discriminator: fieldType
required:
- name
- fieldType # required for inheritance to work
properties:
name:
type: string
fieldType:
type: string
inputField:
allOf:
- $ref: '#/definitions/field' # All properties of a Field
- properties: # extra properties only for fields
placeholder:
type: string
textareaField:
allOf:
- $ref: '#/definitions/field' # All properties of a Field
- properties: # extra properties only for fields
placeholder:
type: string
form:
type: object
required:
- name
properties:
name:
type: string
fields:
type: array
items:
$ref: '#/definitions/field'
はその後、私の春のエンドポイントで、私は次のRESTコントローラがあります。
@Override
public ResponseEntity<Void> addForm(@ApiParam(value = "form object to add to the store", required = true) @RequestBody Form form) {
if(form.getFields().isEmpty()){
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
ch.test.form.database.model.Form formModel = new ch.test.form.database.model.Form();
formModel.setName(form.getName());
for(Field field: form.getFields()){
System.out.println(field);
switch(field.getFieldType()){
case "InputField":
formModel.addField(new InputField(field.getName(), ((ch.test.form.api.dto.InputField)field).getPlaceholder()));
break;
case "TextareaField":
formModel.addField(new TextareaField(field.getName(), ((ch.test.form.api.dto.TextareaField)field).getPlaceholder()));
break;
}
}
formModel.setUser(user);
try{
formRepository.save(formModel);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("location", "/pointScales/" + formModel.getId());
return ResponseEntity.status(HttpStatus.CREATED).headers(responseHeaders).build();
} catch (DataIntegrityViolationException e){
System.out.println(e.getMessage());
System.out.println(e.getClass());
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build();
}
}
を持つ新しい入力フィールドを作成するときに、私はこの例外java.lang.ClassCastException: ch.test.form.api.dto.Field cannot be cast to ch.test.form.api.dto.InputField
を取得次のjson:
{
"fields": [
{
"fieldType": "InputField",
"name": "string",
"placeholder": "test"
}
],
"name": "string"
}
私は何をしていますか?それとも? dtosを作成するときに注釈がありませんか?