在 spring 引导中发布数据时出现问题 Rest Api- org.springframework.web.bind.MethodArgumentNotValidException

Problem while posting data in spring boot Rest Api- org.springframework.web.bind.MethodArgumentNotValidException

我是 spring 开机休息 api 的新手。 我已经创建了一个类别 Rest 控制器,我正在使用它 post 将数据发送到我的后端 mysql 数据库。

我还在我的项目中添加了 spring-jpa 和 hibernate,并且工作正常。 当我使用 Bootstrap 表单和 JqueryAjax posting 数据时,当我点击表单中的提交按钮时,我在 intellij 控制台中得到 org.springframework.web.bind.MethodArgumentNotValidException,在浏览器控制台中得到 400。

我的休息控制器代码

private CategoryRepository categoryRepository;

    //@GetMapping("/Categories")
    @RequestMapping(value="/Categories", method=RequestMethod.GET)
    public Page<Category> getAllCategories(Pageable pageable) {
        return categoryRepository.findAll(pageable);
    }


    //@PostMapping("/Categories")
    @RequestMapping(value="/Categories", method=RequestMethod.POST)
    public Category createCategory( @Valid @RequestBody Category Category) {
        return categoryRepository.save(Category);
    }

我的js-ajax文件

$(document).ready(
    function() {

        // SUBMIT FORM
        $("#Cateform").submit(function(event) {
            // Prevent the form from submitting via the browser.
            event.preventDefault();
            ajaxPost();
        });

        function ajaxPost() {
            // PREPARE FORM DATA
            var formData = {

                CategoryId : $("#CatId").val(),
                CategoryName : $("#CatName").val(),
                CategoryDescription : $("#Catdesc").val()
            }

            // DO POST
            $.ajax({
                type : "POST",
                contentType : "application/json",
                url : "http://localhost:8080/Categories",
                data : JSON.stringify(formData),
                dataType : 'json',
                success : function(result) {
                    if (result.status == "success") {
                        $("#postResultDiv").html(
                            "" + result.data.CategoryName
                               + result.data.CategoryDescription
                            + "Post Successfully! <br>"
                            + "---> Congrats !!" + "</p>");
                    } else {
                        $("#postResultDiv").html("<strong>Error</strong>");
                    }
                    console.log(result);
                },
                error : function(e) {
                    alert("Error!")
                    console.log("ERROR: ", e);
                }
            });

        }
    })

我的Bootstrap表格

<form id="Cateform">

        <div class="form-group">
            <input type="hidden" class="form-control" id="CatId" placeholder="Enter Book Id" name="CategoryId">
        </div>

        <div class="form-group">
            <label for="CatName">Category Name:</label>
            <input type="text" class="form-control" id="CatName"  name="CategoryName">
        </div>

        <div class="form-group">
            <label for="Catdesc">Category Desc:</label>
            <input type="text" class="form-control" id="Catdesc"  name="CategoryDescription">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>

我的品类模型

@Entity
@Table(name = "Categories")
public class Category extends AuditingModel {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer categoryId;
    public String CategoryName;

    @NotNull
    @Size(max = 250)
    public String Description;


 //gettter and setters
}

好处是...我能够使用 swagger-UI 和 PostMan post 数据,但我不知道当我 post 处理数据时发生了什么对于类别模型的我的描述字段,使用表单和获取方法参数不是有效异常。 我已经在模型中将此字段设置为 notnull 但为什么它从 posting 来自 UI 而不是来自 Swagger-UI 和 Postman.

的数据中给出错误

下面是我在表单中点击提交时在 intellij 控制台中遇到的确切错误

** 已解决 [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public edu.ait.shoppingCart.Dto.Category edu.ait.shoppingCart.Controllers.CategoryController.createCategory(edu.ait.shoppingCart.Dto.Category): [Field error in object 'category' on字段 'Description':拒绝值 [null];代码 [NotNull.category.Description,NotNull.Description,NotNull.java.lang.String,NotNull];参数 [org.springframework.context.support.DefaultMessageSourceResolvable:代码 [category.Description,描述];参数 [];默认消息[描述]];默认消息[不得为空]]] ** browser error

请注意,问题是变量名不匹配。 ajax 调用发送 CategoryDe​​scription,而实体期望 Description 还注意 categoryId 上的区分大小写的 C。 Ajax 调用发送大写 C,而实体声明为小写字母 c