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

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 the name attribute in your form inputs doesn't exactly match the property names in your Article class. For example, if your entity has a field articleId but your form uses <input name="article_id"> or <input name="id">, the framework can't bind the data correctly. Double-check every input's name value 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
    Your Article class has an int articleId field—if the form submits an empty string or non-numeric value for this field, the framework can't convert it to an int, 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 to Integer instead of int (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 your Article entity has fields marked with @NotNull or 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 the Article object. If you haven't defined these for all fields, the binding process will fail silently (or throw a 400). Ensure your Article class 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:13:03