Warm tip: This article is reproduced from stackoverflow.com, please click
java spring-boot jackson-databind

Error create @Bean of ObjectMapper in @Service

发布于 2020-03-31 22:53:55

i have this configuration class:

@Configuration
public class ApplicationConfig {

    @Bean
    public ObjectMapper obMapper() {
        return new ObjectMapper();
    }

}

And this in my service:

@Qualifier("obMapper")
private ObjectMapper obMapper;

But i recieve this error:

Parameter 0 of method jacksonCodecCustomizer in org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration$JacksonCodecConfiguration required a single bean

How should i create it?

Questioner
jdflores
Viewed
26
A. Wolf 2020-01-31 19:24

If you want to inject the bean you have to use one of these:

1. Autowire the dependency

@Autowired
@Qualifier("obMapper")
private ObjectMapper objectMapper;

2. Use setter injection

private ObjectMapper objectMapper;
@Autowired
public setObjectMapper(ObjectMapper objectMapper) {
    this.objectMapper = objectMapper;
}

You can find more details for example in this article on Baeldung.