扩展Broadleaf默认管理控制器

时间:2019-03-05 14:35:01

标签: java spring broadleaf-commerce

我想为管理端(broadleaf框架)创建自己的控制器,该控制器扩展了AdminBasicEntityController。  有我的控制器代码:

    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();
    }
}

我还向AdminServletConfig文件中添加了下一个代码:

@Bean
public AdminBasicEntityController adminBasicEntityController() {
    return new AdminArticleController();
}

我有问题:  我的堆栈跟踪

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

我的控制器出了什么问题。我在guide

上看到了该指南

更新 如果将@RequestMapping({"/admin/pages"})添加到类级别,则我的控制器方法将被忽略,并且相同的父类方法也可以工作。为什么?

非常感谢!

1 个答案:

答案 0 :(得分:1)

您非常亲密!要覆盖现有的阔叶Bean,请确保使用注释中声明的Bean名称。像这样将您的bean命名为“ blAdminBasicEntityController”:

@Bean
public AdminBasicEntityController blAdminBasicEntityController() {
    return new AdminArticleController();
}

您看到的错误是由于存在两个具有相同映射的bean。发生这种情况是因为AdminBasicEntityController带有@Controller("blAdminBasicEntityController")注释,这意味着它将由组件扫描拾取并创建一个名为blAdminBasicEntityController的bean。由于AdminArticleController扩展了AdminBasicEntityController,因此一旦创建AdminArticleController Bean,则AdminBasicEntityController中的每个映射都会有2个映射。解决方案是覆盖Broadleaf bean,而不是添加其他bean。