spring - 创建名为 'requestMappingHandlerMapping' Spring Boot 的 bean 时出错

标签 spring spring-mvc spring-boot

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

我花了一些时间摆弄我的 Controller ,但看不到任何重复的映射,因此不完全确定出了什么问题。下面是我的 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";
    }
}

错误日志:

*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$0(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$1(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*

最佳答案

在您的所有请求映射中,您错误地使用了 name 而不是 value

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

应该是

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

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

关于spring - 创建名为 'requestMappingHandlerMapping' Spring Boot 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50569589/

相关文章:

Spring Data JPA save() 抛出 NPE

java - 在anyRequest之后无法配置antMatchers(多个antMatcher)

java - 带有 MockMvcBuilders 的 SpringBootTest 独立设置没有加载我的 ControllerAdvice 尽管设置了它

spring - 事务在多对多中不起作用

java - Spring:RestController 和 Controller 的不同异常处理程序

spring - 使用开放 API 配置设置全局参数?

java - @Cacheput 的实际用例是什么?

Spring RestController 忽略包装类中的 XmlElement 注释

java - Spring Boot/Spring Data 集成测试

spring - 如何查找 Redis 中可用的最大连接数以及使用了多少连接数和免费连接数?