创建名为“requestMappingHandlerMapping”Spring Boot 的 bean 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50569589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:37:00  来源:igfitidea点击:

Error creating bean with name 'requestMappingHandlerMapping' Spring Boot

springspring-mvcspring-boot

提问by Nathan English

I've just created my controller, however when I try to start my application I get the error mentioned in the title.

我刚刚创建了我的控制器,但是当我尝试启动我的应用程序时,我收到了标题中提到的错误。

I've spent some time messing around with my controller and cannot see any duplicated mappings so not entirely sure whats going wrong. Below is my controller:

我花了一些时间弄乱我的控制器,但看不到任何重复的映射,所以不完全确定出了什么问题。下面是我的控制器:

@Controller
public class CSPServerController {


    @Autowired
    ServerService serverService;

    @Autowired
    AuditLogService auditLogService;

    @RequestMapping(name = "/servers", method = RequestMethod.GET)
    @PreAuthorize("hasRole(T(com.nathanenglish.serverlldmanagementtool.config.GlobalConfig).RoleReadOnly)")
    public String loadServers(Model model){

        model.addAttribute("servers",serverService.getAll());

        return "servers";
    }

    @RequestMapping(name = "/servers/new", method = RequestMethod.GET)
    @PreAuthorize("hasRole(T(com.nathanenglish.serverlldmanagementtool.config.GlobalConfig).RoleEdit)")
    public String newServer(Model model){

        model.addAttribute("server", new Server());
        model.addAttribute("auditLog", new AuditLog());

        return "server";
    }

    @RequestMapping(name = "/servers/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasRole(T(com.nathanenglish.serverlldmanagementtool.config.GlobalConfig).RoleEdit)")
    public String getServer(@PathVariable Long id, Model model){

        model.addAttribute("server", serverService.getById(id));
        model.addAttribute("auditLog", new AuditLog());

        return "server";
    }

    @RequestMapping(name = "/servers/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole(T(com.nathanenglish.serverlldmanagementtool.config.GlobalConfig).RoleEdit)")
    public String saveServer(Model model, @Valid Server server, @Valid AuditLog auditLog, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            return "server";
        }

        serverService.save(server);
        auditLogService.save(auditLog);

        return "redirect:/servers";
    }

    @RequestMapping(name = "/servers/delete/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasRole(T(com.nathanenglish.serverlldmanagementtool.config.GlobalConfig).RoleEdit)")
    public String deleteServer(@PathVariable Long id, Model model){

        serverService.deleteByID(id);

        return "redirect:/servers";
    }
}

Error Log:

错误日志:

*org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'CSPServerController' method 
public java.lang.String com.nathanenglish.serverlldmanagementtool.controller.CSPServerController.getServer(java.lang.Long,org.springframework.ui.Model)
to {[],methods=[GET]}: There is already 'CSPServerController' bean method
public java.lang.String com.nathanenglish.serverlldmanagementtool.controller.CSPServerController.newServer(org.springframework.ui.Model) mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean
@RequestMapping(name = "/servers/{id}", method = RequestMethod.GET)
(AbstractBeanFactory.java:317) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at com.nathanenglish.serverlldmanagementtool.ServerLldManagementToolApplication.main(ServerLldManagementToolApplication.java:12) [classes/:na] Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'CSPServerController' method public java.lang.String com.nathanenglish.serverlldmanagementtool.controller.CSPServerController.getServer(java.lang.Long,org.springframework.ui.Model) to {[],methods=[GET]}: There is already 'CSPServerController' bean method public java.lang.String com.nathanenglish.serverlldmanagementtool.controller.CSPServerController.newServer(org.springframework.ui.Model) mapped. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:580) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:544) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:265) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods(AbstractHandlerMethodMapping.java:250) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_171] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:248) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:218) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:188) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:129) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1765) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1702) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] ... 16 common frames omitted*

回答by Ranjith

In all your request mappings, you have incorrectly used nameinstead of value

在您所有的请求映射中,您错误地使用了name代替value

@RequestMapping(value = "/servers/{id}", method = RequestMethod.GET)

should be

应该

@GetMapping(value = "/asset", produces = { MediaTypes.APPLICATION_JSON_UTF8 })
ResponseEntity<ApiResponse<Object>> getAssetData(String assetId) {}

@GetMapping(value = "/asset", produces = { MediaTypes.APPLICATION_JSON_UTF8 })
ResponseEntity<ApiResponse<Object>> getAllAssets() {}

As a result of this, both getServer and newServer were trying to map to the same URL - GET /which is not allowed.

因此, getServer 和 newServer 都试图映射到相同的 URL -GET /这是不允许的。

回答by Bagesh Sharma

Generally, this error comes when you put the same URL mapping with in the same controller or any other controller class for the same method Types. For example -

通常,当您将相同的 URL 映射放入相同的控制器或任何其他控制器类的相同方法类型时,就会出现此错误。例如 -

@GetMapping(value = "/asset", produces = { MediaType.APPLICATION_JSON_VALUE })
    ResponseEntity<ApiResponse<Object>> getAssetData(String assetId) {}

@GetMapping(value = "/asset", produces = { MediaType.APPLICATION_XML_VALUE })
    ResponseEntity<ApiResponse<Object>> getAssetData(String assetId) {}

In this case, we are using the same method type with the same URL mappings which are wrong. This mapping should be unique within all controllers for the same method types.

在这种情况下,我们使用相同的方法类型和错误的相同 URL 映射。对于相同的方法类型,此映射在所有控制器中应该是唯一的。

You can use the same mapping only for HTTP method types like GET, POST, PUT, DELETE but only once.

您只能对 HTTP 方法类型(如 GET、POST、PUT、DELETE)使用相同的映射,但只能使用一次。

But

if Accept Header value (produces) is different for the same mapping than there is no problem for same method also.

如果相同映射的 Accept Header 值(产生)不同,则相同方法也没有问题。

##代码##

It will work fine because "produces" value is different for even the same URL mapping.

它会正常工作,因为即使是相同的 URL 映射,“生产”值也不同。