【问题标题】:multipart/form with backbone and spring boot带有主干和弹簧靴的多部分/形式
【发布时间】:2014-09-27 19:44:49
【问题描述】:
addVendorLicense:function(){
                    var self=this;
                    if(app.vendorId){

                     $('#licenseForm').ajaxSubmit({
                        url: "vendorLicense/create/"+app.vendorId,
                        async:false,
                        success: function (res) {
                            alert(res.message);
                           self.addLicenseRow();
                        },
                        error:function(res){
                            alert(res.message);
                        }
                    });
                    }
                    else{
                        alert("Please Add a vendor First");
                    }   
                    return false;
             },**

***Spring Controller***

@RequestMapping(value = "/create/{vendorId}", method = RequestMethod.POST)
    public ResponseEntity<VendorLicenseResponse> createLicense(LicenseRequest licenserequest,@PathVariable("vendorId") String vendorId) {}

***Request Object***

public class LicenseRequest extends BaseRepresentation {

@JsonIgnore

    private String licenseType;

    private String licenseName;

private String licenseNumber;

    @JsonIgnore
    private String expirationDate;

    @JsonIgnore
    private List<String> statesCovered;

    private String terms;

    private String brokerNumber;

    private String organizationId;

    @JsonIgnore
    private CommonsMultipartFile insuranceFiles;

}

我正在尝试将多部分表单数据映射到 LicenseRequest 对象。当使用文件提交表单时,会正确解析 rquest,但是当没有文件提交表单时,服务器会给出 400 错误请求错误。有人可以告诉我如何制作文件在 multipart/form-data 中是可选的。

【问题讨论】:

    标签: ajax spring backbone.js multipartform-data ajaxform


    【解决方案1】:

    您可以直接抓取它并将其设为可选,而不是将其放入请求对象中,我不确定您是否可以在自定义对象中指定可选部分。

    @RequestMapping(value = "/create/{vendorId}", method = RequestMethod.POST)
    public ResponseEntity<VendorLicenseResponse> createLicense(LicenseRequest licenserequest,@PathVariable("vendorId") String vendorId,
    @RequestParam(value = "insuranceFiles", required = false) CommonsMultipartFile insuranceFiles) {
       if(insuranceFiles != null){}
    }
    

    【讨论】:

    • 感谢您的回复。问题是我正在使用 jquery 表单插件来提交表单。所以当没有选择文件时,插件会在文件对象中发送空字符串。哪个 spring 尝试映射到commonsmultipartFile oject。你能建议如何防止表单插件在没有选择文件时停止发送空字符串。
    猜你喜欢
    • 2016-08-30
    • 2018-02-14
    • 2018-04-03
    • 2014-03-09
    • 1970-01-01
    • 2014-06-27
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多