What I'd like to do seems simple, just handling an object like the following:
The problem is, the object contains an abstract field.
When I add these annotations to the abstract class, the phase 1 works fine.
Request JSON:
{ config: { type: "concreteA", ...}, ...}
REST API:
@RequestMapping(value="/configs", method=RequestMethod.POST)
@ResponseBody
public ResponseEntity<Object> saveConfig(@RequestBody ConfigRequest request)
throws IOException {
...
}
ConfigRequest class:
public class ConfigRequest {
private AbstractConfig config;
// Abbr. other fields, and all getters and setters
}
AbstractConfig class, which is included in ConfigRequest
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.PROPERTY,
property="type",
visible=true)
@JsonSubTypes({@Type(value=ConcreteAConfig.class, name="concreteA")})
public abstract class AbstractConfig {
public AbstractConfig(){}
private String type;
// Abbr. other fields, and all getters and setters
}
Deserialized string:
{"type":"concreteA", ...}
But when I try retrieving and deserializing(Phase 2), the deserialization fails:
16/03/24 17:17:20 ERROR (...Abbr...) org.codehaus.jackson.map.JsonMappingException: Can not construct instance of AbstractConfig, problem: abstract types can only be instantiated with additional type information
RowMapper, which raises the error:
public class BatchRowMapper implements RowMapper<Batch> {
private static ObjectMapper objectMapper = new ObjectMapper();
@Override
public Batch mapRow(ResultSet row, int rowNum) throws SQLException {
Batch batch = new Batch();
try {
// THIS METHOD RAISES THE ERROR
batch.setConfig(objectMapper.readValue(row.getString(CONFIG), AbstractConfig.class));
} catch (ClassCastException|IOException e) {
throw new SQLException(e.toString());
}
return batch;
}
}
I'd like to ser/de an abstract field with the same "type" field, and using only annotaions is my wish... Is it possible? If further information needed, I will be willing. Thank you in advance.
I've found it's the problem what Jackson I used.
When I use jackson of codehause(older), deserialization with JsonTypeInfo doesn't work properly. Jackson of fasterxml works perfectly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.