Reputation: 9934
I have two ObjectMappers configured within a springboot application. I declare these in a configuration class like the following:
@Configuration
public class Config {
@Bean
@Primary
public ObjectMapper getPrimary() {
return new ObjectMapper();
}
@Bean
public ObjectMapper getSecondary() {
return new ObjectMapper();
}
}
The @Primary ObjectMapper works without issue. I'm at a loss in understanding how to get a @RestController to use the secondary ObjectMapper though. Any help is appreciated.
Upvotes: 0
Views: 687
Reputation: 935
you need to use qualifiers:
@Configuration
public class Config {
@Bean("myPrimaryOjectMapper")
@Primary
public ObjectMapper getPrimary() {
return new ObjectMapper();
}
@Bean("mySecondaryOjectMapper")
public ObjectMapper getSecondary() {
return new ObjectMapper();
}
}
and then when injecting:
@Autowired
@Qualifier("mySecondaryOjectMapper")
private ObjectMapper objectMapper;
You can read more for example here: https://www.baeldung.com/spring-qualifier-annotation#qualifierVsPrimary
Upvotes: 0
Reputation: 9934
FWIW, I wasn't able to get it to work with the @RestController implicitly, but I ended up injecting the secondary ObjectMapper into the rest controller, and then directly using the mapper to parse the input.
Upvotes: 0