Is it possible to enforce Converter ( org.springframework.core.convert.converter.Converter) to finish json object mapping?
Json code example:
{
"name": "somename",
"customObject": id
}
where somename – string, id – integer value
mapping to :
@Getter
@Setter
@NoArgConstructor
public class ParentObject{
private String name;
private CustomObject customObject;
}
Converter code example:
@Component
public class CustomObjectConverter implements Converter<String, CustomObject>{
@Autowired
private CustomObjectService customObjectService;
@Override
public CustomObject convert(String arg0) {
Long id = Long.parseLong(arg0);
return customObjectService.findById(id);
}
}
What I want to achieve is to map that json to the object which will have automatically fetched from db nested object.
Advertisement
Answer
You should implement your own JacksonCustomMapper, by extending JsonMapper and then registering it into the set of HttpMessageConverters. But, I do not recommend to pollute the default conversion, you could pass in the @RequestBody an incomplete json and Jackson will parse it to your object, it would be sufficient to not pass wrong keys in your json object… An example (among thousands) here: http://magicmonster.com/kb/prg/java/spring/webmvc/jackson_custom.html. Enjoy it 🙂