简体   繁体   中英

ModelAttribute is null

I use spring boot 2

Controller part

@PostMapping("template/new/samplings")
@ResponseBody
public SamplingsDto save(@ModelAttribute SamplingsDto samplings) {
    return samplingsService.save(samplings);
}

I try to do save a form

$("#samplingsForm").submit(function (e){
    e.preventDefault();

    var receptionDate =  $("#samplingsReceptionDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');
    var buildDate =  $("#samplingsBuildDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');

    var form = transForm.serialize('#samplingsForm');

    form.receptionDate=receptionDate;
    form.buildDate=buildDate;

    form = JSON.stringify(form);

    $.ajax({
        type:"post",
        url: "/template/new/samplings",
        data: form,
        contentType: "application/json",
        dataType : "json",
        success: function(data){
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });

});

With chrome request payload is

"{" "samplingsId":"", "buildDate":"2018-06-20", "receptionDate":"2018-06-20", "productTypesId":"1", "productsId":"15", "}"

On the server model attribute field are null

Edit

public class SamplingsDto {

        private Integer samplingsId;

        private Integer productTypesId;

        private Integer productsId;

        private LocalDate receptionDate;

        private LocalDate buildDate;

        //get set
    }

First, add setters and getters for the properties of SamplingsDto. (I would also use Lombok for setters/getters)

public class SamplingsDto {
    private Integer samplingsId;

    private Integer productTypesId;

    private Integer productsId;

    private LocalDate receptionDate;

    private LocalDate buildDate;

    public Integer getSamplingsId() {
        return samplingsId;
    }

    public void setSamplingsId(Integer samplingsId) {
        this.samplingsId = samplingsId;
    }

    public Integer getProductTypesId() {
        return productTypesId;
    }

    public void setProductTypesId(Integer productTypesId) {
        this.productTypesId = productTypesId;
    }

    public Integer getProductsId() {
        return productsId;
    }

    public void setProductsId(Integer productsId) {
        this.productsId = productsId;
    }

    public LocalDate getReceptionDate() {
        return receptionDate;
    }

    public void setReceptionDate(LocalDate receptionDate) {
        this.receptionDate = receptionDate;
    }

    public LocalDate getBuildDate() {
        return buildDate;
    }

    public void setBuildDate(LocalDate buildDate) {
        this.buildDate = buildDate;
    }
}

Then you can use it as follows:

@PostMapping("template/new/samplings")
public SamplingsDto save(@RequestBody SamplingsDto samplings) {
    return samplingsService.save(samplings);
}

You need to create rest webservice to recieve this request. Add @RestController annotation and set header as application/json . I hope this will help you to figure out your problem here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM