我有一个看起来像这样的JSON对象:
{"name":
{"description": "test",
"another_field": 1,
...
}
...
}
我希望能够将JSON映射到Map<String, Specification>
之类的映射对象,在该对象中规范对象可以将JSON属性名称映射到其他名称,当前,当我用Jackson转换时,我只得到JSON名称。我当前的转换器类如下:
public class HashMapConverter implements AttributeConverter<Map<String, Specification>, String> {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public String convertToDatabaseColumn(Map<String, Specification> techSpecs){
String technicalSpecs = null;
try {
technicalSpecs = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(techSpecs);
} catch (final JsonProcessingException e) {
System.out.println(e);
}
return technicalSpecs;
}
@Override
public Map<String, Specification> convertToEntityAttribute(String techSpecJSON){
TypeReference<Map<String, Specification>> mapType = new TypeReference<Map<String, Specification>>(){};
Map<String, Specification> techSpecs = null;
try {
techSpecs = objectMapper.readValue(techSpecJSON, mapType);
} catch (final IOException e) {
System.out.println(e);
}
return techSpecs;
}
}
Spec类看起来像这样:
public class Specification {
@JsonProperty("description")
public String description;
@JsonProperty("another_field")
public Integer version;
...
}