在Spring Boot中发布数据时遇到的问题Rest Apiorg.springframework.web.bind.MethodArgumentNotValidException

问题描述 投票:0回答:1

我是Spring Boot Rest API的新手。我创建了一个类别Rest控制器,使用它我将数据发布到后端mysql数据库。

我还在我的项目中添加了spring-jpa和hibernate,它的工作正常。当我使用Bootstrap表单和JqueryAjax发布数据时,我在表单中单击“提交”按钮时,在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);
                }
            });

        }
    })

我的引导程序表单

<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发布数据,但是当我使用表单发布数据并获取方法参数不是我类别描述字段的有效异常时,我不知道会发生什么模型。我已经在模型中将此字段设置为notnull,但是为什么它会由于从UI发布数据而不是从Swagger-UI和Postman发布数据而产生错误。

以下是我在击中表单提交时在IntelliJ控制台中遇到的exxact错误>>

**已解决的[org.springframework.web.bind.MethodArgumentNotValidException:在公共edu.ait.shoppingCart.Dto.Category edu.ait.shoppingCart.Controllers.CategoryController.createCategory(edu.ait.shoppingCart.Dto。类别):[字段“描述”中对象“类别”中的字段错误:拒绝值[null];代码[NotNull.category.Description,NotNull.Description,NotNull.java.lang.String,NotNull];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[category.Description,Description];参数[];默认消息[描述]];默认消息[不得为null]]**browser error

我是Spring Boot Rest API的新手。我创建了一个类别Rest控制器,使用它我将数据发布到后端mysql数据库。我还在项目中添加了spring-jpa和hibernate及其...

spring-boot rest spring-restcontroller
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.