spring 项目 String 转换 Long 异常

evaluationId传入三位数的可以,超过四位数就不行了。

错误

13:41:12 DEBUG [http-nio-80-exec-8] o.s.w.s.DispatcherServlet - POST "/enjoy", parameters={masked}
13:41:12 DEBUG [http-nio-80-exec-8] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to com.imooc.reader.controller.MemberController#valuation(Long)
13:41:12 DEBUG [http-nio-80-exec-8] o.s.web.method.HandlerMethod - Could not resolve parameter [0] in public java.util.Map com.imooc.reader.controller.MemberController.valuation(java.lang.Long): Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "2,000"
13:41:12 WARN  [http-nio-80-exec-8] o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "2,000"]
13:41:12 DEBUG [http-nio-80-exec-8] o.s.w.s.DispatcherServlet - Completed 400 BAD_REQUEST

Ajax提交

  //评论点赞
            $("*[data-evaluation-id]").click(function () {
                let evaluationId = $(this).data("evaluation-id");
                $.post("/enjoy", {evaluationId: evaluationId}, function (json) {
                    if (json.code == "0") {
                        $("*[data-evaluation-id='" + evaluationId + "'] span").text(json.evaluation.enjoy);
                    }
                }, "json")
            })

MemberController

    @PostMapping("/enjoy")
    @ResponseBody
    public Map valuation(Long evaluationId) {
        System.out.println("类型:" + evaluationId.getClass() + "值:" + evaluationId);
        Map result = new HashMap();
        try {
            Evaluation evaluation = memberService.enjoy(evaluationId);
            result.put("code", "0");
            result.put("msg", "success");
            result.put("evaluation", evaluation);
        } catch (BussinessException e) {
            e.printStackTrace();
            result.put("code", e.getCode());
            result.put("msg", e.getMsg());
        }
        return result;
    }

 

你检查一下 你传过来的 请求参数 long类型的值  里面带了逗号没有

请求参数错了 java.lang.NumberFormatException: For input string: "2,000"
带了逗号

String 转 Long
@PostMapping("/aa")
	public void a(String a){
		a="2000";
		long l = Long.parseLong(a);
		System.out.println(l);
	}

 

从报错来看是前端展示用了逗号,需要替换后再转long,这不3位4位的问题。