I want to create my own controller for admin side (broadleaf framework), which extends AdminBasicEntityController. There is my controller code:
public class AdminArticleController extends AdminBasicEntityController {
@Autowired
private ArticleService articleService;
@Override
public String addEntity(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @ModelAttribute("entityForm") EntityForm entityForm, BindingResult result) throws Exception {
return super.addEntity(request, response, model, pathVars, entityForm, result);
}
@Override
public String saveEntityJson(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable("id") String id, @ModelAttribute("entityForm") EntityForm entityForm, BindingResult result, RedirectAttributes ra) throws Exception {
return super.saveEntityJson(request, response, model, pathVars, id, entityForm, result, ra);
// articleService.updateArticleEntity();
}
}
I also added the next code to AdminServletConfig file:
@Bean
public AdminBasicEntityController adminBasicEntityController() {
return new AdminArticleController();
}
And i have the problem: My stacktrace
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminControllerHandlerMapping' defined in URL [jar:file:/C:/Users/ntsarkevich/.m2/repository/org/broadleafcommerce/broadleaf-open-admin-platform/6.0.1-GA/broadleaf-open-admin-platform-6.0.1-GA.jar!/org/broadleafcommerce/openadmin/web/controller/AdminControllerHandlerMapping.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'adminBasicEntityController' method
public java.lang.String org.broadleafcommerce.openadmin.web.controller.entity.AdminBasicEntityController.addEntity(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,org.springframework.ui.Model,java.util.Map<java.lang.String, java.lang.String>,org.broadleafcommerce.openadmin.web.form.entity.EntityForm,org.springframework.validation.BindingResult) throws java.lang.Exception
to {[/{sectionKey:.+}/add],methods=[POST]}: There is already 'blAdminBasicEntityController' bean method
public java.lang.String org.broadleafcommerce.openadmin.web.controller.entity.AdminBasicEntityController.addEntity(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,org.springframework.ui.Model,java.util.Map<java.lang.String, java.lang.String>,org.broadleafcommerce.openadmin.web.form.entity.EntityForm,org.springframework.validation.BindingResult) throws java.lang.Exception mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at com.community.admin.AdminApplication.main(AdminApplication.java:12) [classes/:?]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'adminBasicEntityController' method
public java.lang.String org.broadleafcommerce.openadmin.web.controller.entity.AdminBasicEntityController.addEntity(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,org.springframework.ui.Model,java.util.Map<java.lang.String, java.lang.String>,org.broadleafcommerce.openadmin.web.form.entity.EntityForm,org.springframework.validation.BindingResult) throws java.lang.Exception
to {[/{sectionKey:.+}/add],methods=[POST]}: There is already 'blAdminBasicEntityController' bean method
public java.lang.String org.broadleafcommerce.openadmin.web.controller.entity.AdminBasicEntityController.addEntity(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,org.springframework.ui.Model,java.util.Map<java.lang.String, java.lang.String>,org.broadleafcommerce.openadmin.web.form.entity.EntityForm,org.springframework.validation.BindingResult) throws java.lang.Exception mapped.
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:580) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:544) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:265) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$1(AbstractHandlerMethodMapping.java:250) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[?:1.8.0_171]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:248) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:218) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:188) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:136) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
... 16 more
What's wrong with my controller. I see the guide on guide
UPDATE
If I add @RequestMapping({"/admin/pages"})
to class level my controller methods are ignored, and the same parent class methods work. Why?
Many thanks!
You are very close! To override an existing Broadleaf bean, be sure to use the bean name declared in the annotation. Name your bean "blAdminBasicEntityController" like this:
@Bean
public AdminBasicEntityController blAdminBasicEntityController() {
return new AdminArticleController();
}
The error you are seeing is due to there being two beans with the same mapping. This is is happening because AdminBasicEntityController is annotated with @Controller("blAdminBasicEntityController")
, which means it will get picked up by component scanning and create a bean named blAdminBasicEntityController
. Since AdminArticleController extends AdminBasicEntityController, once the AdminArticleController bean is created, there would be 2 mappings for every mapping in AdminBasicEntityController. The solution is to override the Broadleaf bean instead of adding an additional one.