字符串数组请求触发java.lang.NumberFormatException(空字符串输入)
java.lang.NumberFormatException for Empty nicknames Parameter Hey Borgy, let's dig into why that NumberFormatException is popping up when you pass an empty string for the nicknames parameter. This is a classic edge case issue with string-to-array converters, so let's break it down step by step.
What's Causing the Exception?
The error message For input string: "" tells us somewhere in your code, an empty string is mistakenly being treated as a numeric value and trying to be parsed as such. Since your converter works perfectly with valid comma-separated values like "Borgy, Frederick, Carlo", the problem is almost certainly in how your StringArrayConverter (or similar parameter handling logic) processes empty input.
Here's the likely scenario:
- When you pass an empty string, your converter might run
input.split(","), which returns an array containing a single empty string:[""]. - Later in the pipeline (either in the converter itself, or in code that uses the converted array), this empty string gets fed into a method expecting a number (like
Integer.parseInt()), triggering the NumberFormatException.
Step-by-Step Fixes & Debugging
1. Update Your String-to-Array Converter
First, modify your StringArrayConverter to explicitly handle empty or blank input. The goal is to return an empty array instead of an array with an empty string element. Here's a corrected implementation example:
import java.util.Arrays; public class StringArrayConverter { public String[] convert(String input) { // Handle null or blank input first if (input == null || input.trim().isEmpty()) { return new String[0]; // Return empty array instead of [""] } // Split, trim each element, and filter out accidental empty values return Arrays.stream(input.split(",")) .map(String::trim) .filter(element -> !element.isEmpty()) .toArray(String[]::new); } }
This way, empty input gets converted directly to [], which your database's JSON field should handle correctly, and there's no stray empty string left to trigger a numeric parsing error.
2. Verify Request Parameter Binding
If you're using a framework like Spring, check how your nicknames parameter is bound:
- Ensure the parameter is marked as optional with a default empty string if needed:
@GetMapping("/your-endpoint") public ResponseEntity<?> yourMethod(@RequestParam(value = "nicknames", required = false, defaultValue = "") String nicknames) { // ... your logic here } - Confirm your custom converter is properly registered with the framework so it's used to convert the parameter to a
String[].
3. Debug the Conversion Flow
Add logging or debug statements to your converter to track what input it's receiving. For example:
public String[] convert(String input) { System.out.println("Received input for conversion: '" + input + "'"); // Log raw input // ... rest of your conversion logic }
This will confirm whether the empty string is reaching the converter, and help you spot if any unexpected processing happens before the converter runs.
4. Validate Database JSON Field Handling
Double-check that your database's JSON field accepts empty arrays ([]). Most modern databases (PostgreSQL, MySQL with JSON type) support this, but if there's a constraint or custom serializer in place, it might need adjustment to handle empty arrays correctly.
Final Checks
After updating the converter, test these cases to ensure everything works:
- Empty string input (
"") → should convert to[]and save without errors - Valid comma-separated values → continues to work as before
- Partial empty input (e.g.,
" , Borgy,, Frederick ") → should trim and filter to["Borgy", "Frederick"]
内容的提问来源于stack exchange,提问作者Borgy Manotoy

