创建名称为' requestMappingHandlerMapping'春季启动

时间:2018-05-28 15:31:36

标签: spring spring-mvc spring-boot

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

我花了一些时间搞乱我的控制器,看不到任何重复的映射,所以不能完全确定什么是错误的。以下是我的控制器:

$( "#signupForm" ).rules( "remove" ); // Removes all static rules from an element
$( "#signupForm" ).rules( "remove", "boxes" ); // Removes 'boxes' rule;
$( "#signupForm" ).rules( "add", "boxes");

错误日志:

@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";
    }
}

2 个答案:

答案 0 :(得分:5)

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

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

应该是

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

因此,getServer和newServer都试图映射到同一个网址 - GET /,这是不允许的。

答案 1 :(得分:1)

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

@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() {}

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

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