Spring Boot Thymeleaf下拉框触发NumberFormatException问题求助
Alright, let's break down this NumberFormatException issue you're facing when accessing pages with BusinessPartner dropdowns. The root cause here is almost always that somewhere in your stack, that prefixed string ID (like BPID_04a3e35b-12b3-447f-a982-338739e537f1) is being treated as a numeric type (Long/Integer) instead of a String. Let's walk through the fixes step by step.
1. Verify Your Entity & Custom Generator Setup First
First, double-check that your BusinessPartner entity's ID is explicitly typed as String, and your custom UUIDGenerator is correctly producing a string value.
Correct Entity ID Mapping
@Entity public class BusinessPartner { @Id @GeneratedValue(generator = "bpid-generator") @GenericGenerator( name = "bpid-generator", strategy = "com.yourpackage.CustomUUIDGenerator", // Replace with your actual package parameters = { @Parameter(name = "prefix", value = "BPID_") } ) private String id; // Critical: Must be String, not Long/Integer // Other fields, getters, setters }
Custom UUIDGenerator Implementation
Make sure your generator returns a String (not a numeric UUID) and correctly appends the prefix:
public class CustomUUIDGenerator extends UUIDGenerator { private String prefix; @Override public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException { super.configure(type, params, serviceRegistry); this.prefix = params.getProperty("prefix"); } @Override public Serializable generate(SessionImplementor session, Object object) throws HibernateException { // Get the standard UUID as a string, then prepend your prefix String baseUuid = (String) super.generate(session, object); return prefix + baseUuid; } }
2. Fix Controller Parameter Binding
The most common trigger for this exception is a controller method expecting a numeric ID instead of a String. For example, if your dropdown submits the ID to a controller, check that the parameter type is String:
Wrong (Causes Exception)
// This will throw NumberFormatException when receiving "BPID_xxx" @PostMapping("/save-partner") public String savePartner(@RequestParam("partnerId") Long partnerId, Model model) { // ... }
Correct
@PostMapping("/save-partner") public String savePartner(@RequestParam("partnerId") String partnerId, Model model) { BusinessPartner partner = partnerService.findById(partnerId); // ... rest of your logic }
3. Fix Frontend Dropdown Rendering
If you're using a template engine like Thymeleaf or JSP, ensure the dropdown's value attribute is correctly bound to the string ID without any numeric conversion.
Thymeleaf Example (Correct)
<select name="partnerId" class="form-control"> <option th:each="partner : ${businessPartners}" th:value="${partner.id}" th:text="${partner.name}"> </option> </select>
Avoid any numeric formatting functions on the ID (like ${#numbers.formatInteger(partner.id, 0)})—these will try to parse the string as a number and throw the exception.
4. Check Hibernate Mapping Configuration
Ensure your ID column in the database is defined as a VARCHAR (not BIGINT/INT), and your entity's @Column annotation matches:
@Id @GeneratedValue(generator = "bpid-generator") @GenericGenerator(/* ... */) @Column(name = "id", columnDefinition = "VARCHAR(60)", nullable = false) private String id;
5. Debug the Exception Stack Trace
If you're still stuck, look closely at the exception stack trace to pinpoint exactly where the conversion is failing:
- If it's in
org.springframework.beans.TypeConverterDelegate, it's a controller parameter binding issue. - If it's in your template engine's code, it's a frontend rendering issue.
- If it's in Hibernate's code, check your entity mapping or generator.
内容的提问来源于stack exchange,提问作者Satvik Gadag

