我有一个休息服务来更新实体。作为Web界面,我使用带有Jackson的Spring Web来处理请求正文。
@PutMapping(value = "/save")
public ResponseEntity<TestDefinitionFullDto> saveForm(@RequestBody TestDefinitionFullDto definitionFullDto) {
Optional<TestDefinitionFullDto> result = testFormService.save(definitionFullDto);
return result
.map(testDefinition -> new ResponseEntity<>(testDefinition, HttpStatus.OK))
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
我想将请求正文反序列化为该对象。
@JsonIgnoreProperties
public class TestDefinitionFullDto {
private UUID uuid;
private String name;
private String description;
private List<String> groups;
public TestDefinitionFullDto() {}
public TestDefinitionFullDto(TestFormDefinitionEntity entity) {
this.uuid = entity.getUuid();
this.name = entity.getName();
this.description = entity.getDescription();
this.groups = entity.getGroups();
}
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<String> getGroups() {
return groups;
}
public void setGroups(List<String> groups) {
this.groups = groups;
}
}
问题在于请求正文中的json包含 groups 属性,这是另一个json,但是我想在将其作为字符串处理并将其反序列化为字符串列表时保留它。
{
"uuid":"75635347-88b1-4d0e-b72f-c54d6398461b",
"name":"awfwaf",
"description":"",
"groups":[
{
"_id":0,
"_name":"",
"_questions":[
{
"_id":1,
"_name":"",
"_questionFields":[
]
},
{
"_id":2,
"_name":"",
"_questionFields":[
]
},
{
"_id":3,
"_name":"",
"_questionFields":[
]
},
{
"_id":4,
"_name":"",
"_questionFields":[
]
},
{
"_id":5,
"_name":"",
"_questionFields":[
]
}
]
}
]
}
错误输出:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@66c1675e; line: 1, column: 91] (through reference chain: cz.rusna.core.forms.views.TestDefinitionFullDto["groups"]->java.util.ArrayList[0])
是否可以将组反序列化为String而不是对象?