0

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...

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
LeO
  • 4,238
  • 4
  • 48
  • 88
  • well you are creating the `ObjectMapper` by hand - via `new`, so that `@Service` is obviously not present. I think this will work though if you get the `ObjectMapper` from Spring Context - for example via `@Autowired` – Eugene Jun 13 '18 at 09:33
  • In principal the idea sounds nice - **BUT** I have no clue how to do it practically. Ie. `@Autowired` for `ObjectMapper` does NOT work. Looking for some help I cannot figure out how to get _exactly_ the same Spring instance for deserialization :-/ Cuz only the exact would have the `@Autowired` object initiated properly. – LeO Jun 13 '18 at 12:21
  • what spring version are you using? Lots of people say it works with `@Autowired` (so is my project for example)... https://stackoverflow.com/questions/30060006/how-do-i-obtain-the-jackson-objectmapper-in-use-by-spring-4-1/30060377 – Eugene Jun 14 '18 at 08:09
  • Spring MVC, 5.0.7.... Following the advice as you mention I get the Exception at startup `No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected at least 1 ...` which is :-/ And the other approaches from the same answer won't work neither :-/ Or at least I have no clue how to make it working :-( – LeO Jun 14 '18 at 08:46
  • well, I can't really help you, no idea of your setup and why that would not work :| – Eugene Jun 15 '18 at 08:38

0 Answers0