Spring MVC表单传递对象至Controller时持续报400错误求助
Hey Steve, let's break down this 400 Bad Request issue you're facing when submitting your article form. Since you mentioned a possible mismatch between the form data and your Article entity, let's walk through the most common culprits and fixes here.
Common Reasons for Form-Entity Mismatch & Fixes
Field Name Discrepancies
The most frequent issue is that thenameattribute in your form inputs doesn't exactly match the property names in yourArticleclass. For example, if your entity has a fieldarticleIdbut your form uses<input name="article_id">or<input name="id">, the framework can't bind the data correctly. Double-check every input'snamevalue to ensure it aligns perfectly with the entity's field names (note: camelCase vs snake_case matters unless you've configured automatic conversion).Data Type Mismatches
YourArticleclass has anint articleIdfield—if the form submits an empty string or non-numeric value for this field, the framework can't convert it to anint, triggering a 400. If this field should allow null values (e.g., when creating a new article where the ID is auto-generated), change the type toIntegerinstead ofint(since primitive types can't be null). For other fields (like dates), ensure the form's input format matches what the entity expects (add@DateTimeFormat(pattern = "yyyy-MM-dd")to date fields if needed).Missing Required Fields
If yourArticleentity has fields marked with@NotNullor the database table has non-null constraints, failing to submit those fields will cause a 400. Check your entity's annotations (like@Column(nullable = false)) and make sure the form includes all mandatory fields with valid values.Missing Getters/Setters
The framework relies on getter and setter methods to inject form data into theArticleobject. If you haven't defined these for all fields, the binding process will fail silently (or throw a 400). Ensure yourArticleclass has proper getters and setters for every property that's being submitted via the form.
How to Diagnose Exact Errors
To pinpoint the exact issue, add a BindingResult parameter to your controller method—it will capture detailed binding errors:
@RequestMapping("/addArticle") public String addArticle(@ModelAttribute Article article, BindingResult result) { if (result.hasErrors()) { // Print errors to console for debugging result.getAllErrors().forEach(error -> System.out.println(error.getDefaultMessage())); return "article-form"; // Return to form page to fix issues } // Proceed with saving the article return "success"; }
When you submit the form again, the console will show specific errors like "Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'articleId'", which tells you exactly what's wrong.
Quick Check Step
Before diving into code, open your browser's DevTools (F12), go to the Network tab, resubmit the form, and inspect the Form Data section. Verify that every field name and value matches what your Article entity expects.
内容的提问来源于stack exchange,提问作者SteveHu

