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 aLongin your model. - If "TEST" is actually a valid business identifier (not a numeric ID), change your controller parameter to
Stringinstead, 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
nameattribute of your form inputs matches the field names in yourtaskSchJobmodel. - 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
@PathVariabletype toStringand 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:
- Update your frontend to send a valid numeric value, or
- Adjust your controller to accept a
Stringparameter and handle the "TEST" case explicitly.
内容的提问来源于stack exchange,提问作者Amir Asyraf

