天天看点

Missing URI template variable ‘employeeNumber‘ for method parameter of type String

使用SpringMVC参数注解@PathVariable时出错提示:

Missing URI template variable ‘employeeNumber’ for method parameter of type String

/**
     * 通用上传请求
     */
    @PostMapping("/common/upload1")
    @ResponseBody
    public AjaxResult uploadFile(List<MultipartFile> file, @PathVariable ("merNo") String merNo) throws Exception
    {
        try
        {
          .....
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
           
  • 如果@RequestMapping中表示为”item/{id}”,id和形参名称一致,@PathVariable不用指定名称。如果不一致,例如”item/{ItemId}”则需要指定名称@PathVariable(“itemId”)。
  • 因此原代码中的参数@RequestMapping(value = "/findUserByEmployeeNumber/{EmployeeNumber}中{EmployeeNumber}变量名需要和@PathVariable @Valid String employeeNumber中一样

修改办法:

1、方法一:修改PostMapping,带参数

@PostMapping("/common/upload1/{merNo}")
    @ResponseBody
    public AjaxResult uploadFile(List<MultipartFile> file, @PathVariable ("merNo") String merNo) throws Exception
    {
        try
        {
          .....
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
           

2、方法二:将 @PathVariable 修改为@RequestParam

/**
     * 通用上传请求
     */
    @PostMapping("/common/upload1")
    @ResponseBody
    public AjaxResult uploadFile(List<MultipartFile> file, @RequestParam("merNo") String merNo) throws Exception
    {
        try
        {
            ....
            AjaxResult ajax = AjaxResult.success();
               /* ajax.put("fileName", fileName);
                ajax.put("url", url);*/
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
           

注意两个区别

@PathVariable是获取url上数据的。

@RequestParam获取请求参数的(包括post表单提交)

继续阅读