You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot按钮点击传值至控制器报错:类型转换失败排查求助

Hey there! Let's break down this error and figure out what's going wrong.

What the Error is Telling You

The core issue here is that Spring is trying to convert the string value 'TEST' into a Long type (which is required by your taskSchJob model), and obviously "TEST" isn't a valid number—hence the NumberFormatException. This then cascades into a failure to convert the string into your taskSchJob object entirely.

Common Causes & Fixes

1. Mismatched Controller Parameter vs. Frontend Data

First, check how your controller method is set up. You probably have something like one of these two scenarios:

Scenario 1: Expecting a taskSchJob object

@PostMapping("/submit-task")
public String handleButtonClick(@ModelAttribute taskSchJob job) {
    // Your logic here
}

If your taskSchJob model has a Long field (like id), and your frontend is passing "TEST" for that field (e.g., in a form input), Spring can't convert the string to a number.

Scenario 2: Expecting a Long directly

@GetMapping("/process-task")
public String processTask(@RequestParam Long jobId) {
    // Your logic here
}

If you're passing "TEST" as the jobId parameter in your request, Spring can't parse that string into a Long.

Fix for this:

  • Double-check your frontend code (form inputs, AJAX calls, button link parameters) to make sure you're sending a valid numeric value (like 123) instead of "TEST" for the field that maps to a Long in your model.
  • If "TEST" is actually a valid business identifier (not a numeric ID), change your controller parameter to String instead, then handle the logic internally:
    @GetMapping("/process-task")
    public String processTask(@RequestParam String jobIdentifier) {
        if ("TEST".equals(jobIdentifier)) {
            // Handle the TEST case specifically
        } else {
            // Try converting to Long for other cases
            try {
                Long jobId = Long.parseLong(jobIdentifier);
                // Process using the numeric ID
            } catch (NumberFormatException e) {
                // Handle invalid input gracefully
            }
        }
    }
    

2. Misaligned Frontend Parameter Names or Types

If you're using a form or AJAX to send data, make sure:

  • The name attribute of your form inputs matches the field names in your taskSchJob model.
  • For numeric fields, use an input type that enforces numbers:
    <!-- Good: Enforces numeric input -->
    <input type="number" name="id" value="1001">
    
    <!-- Bad: Allows arbitrary text like "TEST" -->
    <input type="text" name="id" value="TEST">
    
  • If using AJAX, ensure your data payload sends the correct type:
    // Wrong: Sends string "TEST" for a numeric field
    $.ajax({
        url: "/submit-task",
        method: "POST",
        data: { id: "TEST" },
        success: () => { /* ... */ }
    });
    
    // Right: Sends a numeric value, or uses a matching string parameter
    $.ajax({
        url: "/submit-task",
        method: "POST",
        data: { id: 1001 }, // Or jobKey: "TEST" if using a string parameter
        success: () => { /* ... */ }
    });
    

3. Path Variable Mismatch

If you're using a path variable in your controller:

@GetMapping("/tasks/{jobId}")
public String getTask(@PathVariable Long jobId) {
    // Your logic here
}

And your request URL looks like /tasks/TEST, Spring will fail to convert "TEST" to a Long.

Fix for this:

  • Ensure the path variable in your URL is a valid number, or change the @PathVariable type to String and handle the logic internally.

Quick Recap

The root problem is that your frontend is sending the string "TEST" where Spring expects a numeric Long (either directly or as part of your taskSchJob model). You need to either:

  1. Update your frontend to send a valid numeric value, or
  2. Adjust your controller to accept a String parameter and handle the "TEST" case explicitly.

内容的提问来源于stack exchange,提问作者Amir Asyraf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:19:21