I have an Object doc
which has its own Deserializer annotated, i.e.
@JsonSerialize(using = DocDeSerializer.class)
public class Doc {
and the Deserializer looks like
@Service
public class DocDeserializer<T> extends JsonDeserializer<Doc> i{
@Autowired
private PropertyController ctrlProp;
@Override
public Doc deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
...
The complete deserialization for HTTP-requests works fine. But now I have the challenge to convert a String into a doc
. My approach with
final ObjectMapper objectMapper = new ObjectMapper();
final Doc myDoc = objectMapper.readValue(strContent, Doc.class);
fails. The reason is that the autowired classes are not initiated properly, i.e. ctrlProp
is not initiated (=null
). So, my question is:
Is there a simple way to reuse the Deserializer from Spring? And if so, how?
UPDATE:
After investigations I found out that it has to do with the config of a HttpConverts. So my config looks like:
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
@Primary
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
converter.setObjectMapper(builder.build());
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
....
converters.add(mappingJackson2HttpMessageConverter());
}
}
which is used in
@RestController
public class MyClass
@Autowired
private MappingJackson2HttpMessageConverter myJackson;
but somehow this fails as well :-( Just for the sake of clarity what was tried...