Good memory - bad written: controller- way of receiving parameters and precautions

Parameter passing mode for a long time unused easily forgotten or memory disturbances, although very simple, but some small details are often beat you.

Xiao Bian decided to take them recorded.

1): @ RequestBody reception parameters and notes

Precautions:

@RequestBody: Background receives only a statement, and can only receive json

@RequestBody: and can form / data coexist

@RequestBody: must be: contentType: "application / json; charset = utf-8" Note: "application / json charset = utf -8" JSON normally can not be resolved

Case: ajxa request

po:

public class RequestBodyPo implements Serializable {

    private Integer id;
    private String name;
    private List<RequestBodyPo> requestBodyPoList;
    private List<RequestBodyPo> BodyPos;

    ... get set 省略...
    
}

request:

$ ( "# canshu" ) .click (function () { 

            var _json = { 
                ID: "123456" , 
                name: "asdasdas" 
            }; 
            // observe requestBodyPoList & BodyPos naming received data back when you will see the results 
            = _json.requestBodyPoList []; 
            _json.BodyPos = []; 

            var _01 = {ID: ". 1", name: "_01" }; 
            var _02 = {ID: "2", name: "_02" }; 

            _json. requestBodyPoList.push (_01); 
            _json.requestBodyPoList.push (_02); 

            _json.BodyPos.push(_01); 
            _json.BodyPos.push(_02);

            $.ajax({
                type: "post",
                url: "/requestBody",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(_json),
                success: function (data) {
                    console.log(data);
                },
                error: function (jqXHR) {
                    alert("发生错误:" + jqXHR.status);
                }
            });
        });

controller receives:

2): controller String [] array, List <String> receiving a set of parameters and considerations

注意事项:

@RequestParam:接收单个字段 而非对象实体

@RequestParam:使用数组接收可以使用 此注解标识  || @RequestBody 注解,区别前者可以定义多个、后者只能定义一个,并且两者的请求格式不一样

案例:ajxa 请求

var _json = ["1","2","3"];

$.ajax({
    type: "post",
    url: "/array",
    dataType: "json",
    contentType:"application/x-www-form-urlencoded",//默认值
    data: {
        strings:_json
    },
    success: function (data) {
        console.log(data);
    },
    error: function (jqXHR) {
        alert("发生错误:" + jqXHR.status);
    }
});

controller 接收:

上述演示的是 @RequestParam 接收数组、要是 list 直接换成 @RequestParam(value = "lists[]") List<String> lists 即可

如果使用:@RequestBody 改成 @RequestBody List<String> lists  再次提醒:使用此注解一定注意请求格式 及 contentType 类型

3):controller 接收 List<Map<String,Object>>集合参数  及 注意事项

注意事项:

使用 @RequestBody 方式接收

案例:ajxa 请求

var _json = [];

var _01 = {
    id: 123,
    name: "小明",
    list: [{
        id: 456,
        name: "翠花"
    }]
};
_json.push(_01);


$.ajax({
    type: "post",
    url: "/listMap",
    dataType: "json",
    contentType: "application/json;charset=utf-8",//默认值  application/x-www-form-urlencoded
    data: JSON.stringify(_json),
    success: function (data) {
        console.log(data);
    },
    error: function (jqXHR) {
        alert("发生错误:" + jqXHR.status);
    }
});

controller 接收:

 

其他补充要点:

@RequestParam:还可以设置 默认参数 和 可选参数  (  @RequestParam(value = "str",defaultValue = "123",required = false) String str  )

controller:还可以接收:

对象实体、List<实体> 等   注意请求格式 contentType

Guess you like

Origin www.cnblogs.com/codingmode/p/12165819.html