json-view icon indicating copy to clipboard operation
json-view copied to clipboard

JsonViewSupportFactoryBean required a single bean, but 2 were found

Open markoperic22 opened this issue 7 years ago • 5 comments

Hi,

I have problems using this library as I'm getting message:

Description:

Field adapter in com.monitorjbl.json.JsonViewSupportFactoryBean required a single bean, but 2 were found: - requestMappingHandlerAdapter: defined by method 'requestMappingHandlerAdapter' in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class] - repositoryExporterHandlerAdapter: defined by method 'repositoryExporterHandlerAdapter' in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]

What should I do in order to fix this?

Thanks

markoperic22 avatar Mar 15 '17 08:03 markoperic22

I don't really have a good answer for this. Spring configurations can be complex, but the root cause is that you've defined a bean twice. Are you importing any other configurations that also define this bean class?

monitorjbl avatar Apr 12 '17 02:04 monitorjbl

I had the exact same problem, I'm trying to configure this on a spring boot project.

maisonChaves avatar Jun 18 '17 18:06 maisonChaves

Same answer. It's not really possible for me to debug this for you because Spring configurations can be complex. You've got two places in your code where this bean (RequestMappingHandlerAdapter) is being picked up. You'll have to step through your Spring configs to figure out where it's coming from.

monitorjbl avatar Jun 20 '17 02:06 monitorjbl

Hello , I am facing the same issue. Did somebody find a way to solve it?

jmesagu1 avatar Jul 19 '17 17:07 jmesagu1

I've solved this issue by adding the following lines in my Application class:

@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

@Bean
@Primary
public RequestMappingHandlerAdapter adapter(){
    return requestMappingHandlerAdapter;
}

@Bean
public JsonViewSupportFactoryBean views(){
   return new JsonViewSupportFactoryBean();
}

By providing a new RequestMappingHandlerAdapter bean annotated as @Primary the JsonViewSupportFactoryBean will pick up this bean.

I'm relatively new to Spring so I don't know why my Application is able to select one of the two RequestMappingHandlerAdapter beans and the JsonViewSupportFactoryBean isn't and I also don't know whether this approach has some side effects. So far, everything works fine.

ThomasJejkal avatar Apr 13 '18 07:04 ThomasJejkal