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

博客介绍了使用SpringMVC参数注解@PathVariable时出现的错误提示,指出@RequestMapping中变量名与形参名称一致时,@PathVariable可不指定名称,不一致则需指定。还给出了修改办法,如修改PostMapping带参数、将@PathVariable改为@RequestParam,并说明了二者区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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表单提交)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值