SpringBoot 415状态参数传递问题

普通传参:

前台:

$.ajax({
    type: "post",
    url: ctxPath+"/pmtm01/pmtm01Save_action",
    data: parems,
    dataType: "json",
    success: function (data) {
        if (data.flag==1) {
            kenWindowClose(true);
            var callback = eval("window.parent.LoadData1");
            callback("操作成功!");

        }
        else{
            GysAlert({
                content: data.message
            });
        }
    },
    error: function (e) {
        alertConsole();
    }
});

后台:

@RequestMapping("/pmtm01Save_action")
@ResponseBody
public ResponseMap Pmtm01SaveAction(String taskNo,String prodName , String tbipm03Id,String interWallId,String outerWallId) {
    ResponseMap req=new ResponseMap();
    try {
        req.setFlag(1);

        //首先获取登陆人
        String userName= WebHelper.GetCurrentUser().getUserName();


        pmtm01Service.insertTpmtm02Info(taskNo,prodName,tbipm03Id,interWallId,outerWallId,userName);

        req.setLogContent("订单收池 任务单"+taskNo+"已进行工艺路径选择");
        req.setType(LogTypeEnum.TypeAdd.getValue());


    } catch (Exception ex) {
        req.setFlag(0);
        req.setMessage("修改数据失败:"+ex.getMessage());
        req.setE(ex);
    }
    return req;
}

参数是一个个列出来的

但是springBoot支持后台接受entity

但是用上面的配置会报出415 参数不支持

需要改动:

前台:在ajax中添加

contentType: "application/json;charset=utf-8",
将data中的对象转换成json格式:
JSON.stringify({projectId: 1,mouldsNo:"nihao",buildId:1,floor:1,prodName:"asdf",page:1,pageSize:1}),

后台:

在controller的参数中添加RequestBody

@RequestMapping(value = "/getProdInfoBySearch")
@ResponseBody
public ResponseMap getProdInfoBySearch(@RequestBody SearchInfo searchInfo){

SearchInfo接受参数entity中,将所有参数改为包装类型。



///

前台传递对象数组到后台,controller直接接受List<对象>作为入参的例子:

前台页面:

var carInfoArr = [];
var modelArray = _get_grid_select_model("#grid");

if(modelArray===undefined||modelArray.length===0){
    GysAlert({content:"当前未选择任何车辆!"});
    return false;
}
for(var i=0;i;i++){
    var carInfo={};
    carInfo.carName=modelArray[i].carName;
    carInfo.carModel = modelArray[i].carModel;
    carInfo.id = modelArray[i].id;
    carInfo.num = modelArray[i].defultNum;
    carInfoArr.push(carInfo);
}

//执行Ajax请求进行添加默认选择车型值
$.ajax({
    type: "post",
    url:ctxPath+"/sl/addDefaultCar_action",
    data:JSON.stringify(carInfoArr),
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function (data) {
        if (data.flag === 1) {
            kenWindowClose(true);
            var callback = eval("window.parent.LoadData1");
            callback("操作成功!");
        }
        else {
            GysAlert({
                content: data.message
            });
        }
    },
    error: function (e) {
        alertConsole(e);
    }
});


后台页面:

@RequestMapping("/addDefaultCar_action")
@ResponseBody
public ResponseMap addDefaultCarAction(@RequestBody List carInfoJson){
    ResponseMap resp = new ResponseMap();

    try {
        boolean flag = slService.addDefultCar(carInfoJson);
        if(flag){
            resp.setFlag(1);
            resp.setMessage("操作成功!");
            resp.setLogContent("智能装车 已成功添加默认出车辆信息");
            resp.setType(LogTypeEnum.TypeAdd.getValue());
        }
        else{
            resp.setFlag(2);
            resp.setMessage("默认车辆添加失败!");
        }
    } catch (Exception e) {
        resp.setFlag(0);
        resp.setE(e);
    }
    return resp;
}
public class AddDefultCarEntity implements Serializable{

    private static final long serialVersionUID = -4764882133687460382L;

    //车辆名称
    private String carName;

    //车辆型号
    private String carModel;

    //车辆id
    private Integer id;

    //车辆选择数量
    private Integer num;

你可能感兴趣的:(日记)