在使用 jersey restful 时 前端ajax post 数据到 服务器端,接收对象为null,测试代码 web: var ts = []; //模拟两条数据for (var i = 0; i 2; i++) {ts.push({name:'name'+i,id:i});}var testData = {aa:ts};printLog('log',t
在使用 jersey restful 时 前端ajax post 数据到 服务器端,接收对象为null,测试代码
web:
var ts = []; //模拟两条数据 for (var i = 0; i < 2; i++) { ts.push({name:'name'+i,id:i}); } var testData = {aa:ts}; printLog('log',testData); $.rate.post({ url: hostUrl + '/xxx/xxx/xxxx/queryDepartmentsAll', data:testData, cache: false, objRest:false, success: function(data) { console.log(data); } });
post data:
--- log --- {"aa":[{"name":"name0","id":0},{"name":"name1","id":1}]}
server:
/** * 查询所有科室 * @return * @throws Exception */ @POST @Path("/queryDepartmentsAll") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String queryDepartmentsAllTest(@Form(prefix="aa")List<TestBean> beans) throws Exception { loggin.info(beans); return "test..."; }
TestBean:
@XmlRootElement public class TestBean { @FormParam("name") private String name; @FormParam("id") private Integer id; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
有请 "debug " 出场.......
ajax发出请求后 会经过 HttpServletInputMessage 处理,获取表单带的参数
调用getDecodedFormParameters,会把 url中的参数和表单参数合并,放入MultivaluedMapImpl 对象中
MultivaluedMapImpl点toString - --> {aa[1][id]=[1], aa[0][name]=[name0], aa[0][id]=[0], aa[1][name]=[name1]}
往下走,到了ListFormInjector
form 注入器有两种,List,Map
最终走到了PrefixedMultivaluedMap 类
对象注入的方式是通过“变量[0].属性” 注入的,而前段传进的值是
解决办法:
把花括号改成“ .” 就可以注入了
修改后:
aa[0].id=0 aa[0].name=name0 aa[1].id=1 aa[1].name=name1
后端接收到了
====================================================================
jquery ajax data 如果对象中包涵数组,是使用 jQuery.param 函数格式化,所以导致 传输的数据格式不正确
jQuery.param = function( a, traditional ) { var prefix, s = [], add = function( key, value ) { // If value is a function, invoke it and return its value value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value ); s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value ); }; // Set traditional to true for jQuery <= 1.3.2 behavior. if ( traditional === undefined ) { traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional; } // If an array was passed in, assume that it is an array of form elements. if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) { // Serialize the form elements jQuery.each( a, function() { add( this.name, this.value ); }); } else { // If traditional, encode the "old" way (the way 1.3.2 or older // did it), otherwise encode params recursively. for ( prefix in a ) { buildParams( prefix, a[ prefix ], traditional, add ); } } // Return the resulting serialization return s.join( "&" ).replace( r20, "+" ); };
可以在请求前把data类型先处理,demo js
var datas = {}; for (var a in settings.data) { if ($.isArray(settings.data[a])) { var isObject = $.isPlainObject(settings.data[a][0]); //如果数组里是对象 if (isObject) { for (var i = 0, ds = settings.data[a], len = ds.length; i < len; ++i) { for (var b in ds[i]) { datas[a + '[' + i + '].' + b] = ds[i][b]; } } } } else {// datas[a] = settings.data[a]; } }