spring中自动注入前端传递的各种类型的数据list/map/bean/array

spring中自动注入前端传递的各种类型的数据list/map/bean/array

在使用springboot开发的过程,如果可以灵活的使用自动注入属性值的方式来获取前端传递的数据可以有效的节约开发时间、规范参数格式。
下面记录我在开发中使用和遇到的问题以及问题的解决办法,不说废话直接上代码:

这种通过设置contentType为application/json类型的方式可以很方便的传递和接受数据,但是弊端是无法在后台通过request.getParameter的方式获取请求数据,代码如下

json类型js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="application/javascript" src="//cdn.bootcss.com/jquery/3.0.0-beta1/jquery.js"></script>
<script type="application/javascript">
$(function(){
var user = {}
var arr = new Array();
arr.push(1);
arr.push(2);
var map = {a: 123, b:898};
user.name = "张三";
user.map = map;
user.list = arr;
var data = JSON.stringify(user);
console.log(data);

$.ajax({
type:'POST',
url:'/bb/aj',
data:data,
contentType: 'application/json',
success:function(data){
alert(data)
}
})

})
</script>
</head>
<body class="showmenu">
travel-web1
</body>
</html>
json类型javaBean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.bilibili.travel.model;

import com.bilibili.base.BaseEntity;

import java.util.List;
import java.util.Map;

/**
* Created by bls on 16-4-30.
*/
public class User extends BaseEntity {

private String name = null;
private Map<String, Object> map = null;
private List<String> list = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Map<String, Object> getMap() {
return map;
}

public void setMap(Map<String, Object> map) {
this.map = map;
}

public List<String> getList() {
return list;
}

public void setList(List<String> list) {
this.list = list;
}

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", map=" + map +
", list=" + list +
'}';
}
}
json类型controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.bilibili.travel.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* Created by bls on 16-4-30.
*/
@Controller
@RequestMapping(value = "/bb")
public class BBController extends BaseSpringController {

@ResponseBody
@RequestMapping(value = "/aj")
public User addJson(@RequestBody User user){
System.out.println(user);
return user;
}

}

这种通过设置contentType为常规application/x-www-form-urlencoded类型的方式虽然前端封装传值不是很美观,但是后台可以通过request.getParameter的方式获取请求数据,在处理身份验证、权限控制、统一数据处理的时候很实用。代码如下

常规类型js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="application/javascript" src="//cdn.bootcss.com/jquery/3.0.0-beta1/jquery.js"></script>
<script type="application/javascript">
$(function () {
$.ajax({
type: 'POST',
url: '/mng/company/save',
data: "companyName=aa&companyKey=asdkelfj&fieldJsonList[0].dbId=12",
success: function (data) {
alert(data)
}
})

})
</script>
</head>
<body class="showmenu">
</body>
</html>
常规类型bean-FieldJson
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.chinaredstar.marketing.dto.tagJson;

import java.io.Serializable;

/**
* tag包含的json数据结构
*/
public class FieldJson implements Serializable {

/**
* 字段所属数据库ID
*/
private Long dbId;

/**
* 字段所属数据库名称
*/
private String dbName;

/**
* 字段所属表ID
*/
private Long tableId;

/**
* 字段所属表名称
*/
private String tableName;

/**
* 字段ID
*/
private Long fieldId;

/**
* 字段名称-表中字段名称
*/
private String fieldName;

/**
* 字段显示名称
*/
private String fieldShowName;

/**
* 字段Model
* 0<{当前时间} - field < 100
*/
private String fieldModel;

/**
* 字段和下个字段之间的关系
*/
private String nexFieldRelation;


public Long getDbId() {
return dbId;
}

public void setDbId(Long dbId) {
this.dbId = dbId;
}

public String getDbName() {
return dbName;
}

public void setDbName(String dbName) {
this.dbName = dbName;
}

public Long getTableId() {
return tableId;
}

public void setTableId(Long tableId) {
this.tableId = tableId;
}

public String getTableName() {
return tableName;
}

public void setTableName(String tableName) {
this.tableName = tableName;
}

public Long getFieldId() {
return fieldId;
}

public void setFieldId(Long fieldId) {
this.fieldId = fieldId;
}

public String getFieldName() {
return fieldName;
}

public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}

public String getFieldShowName() {
return fieldShowName;
}

public void setFieldShowName(String fieldShowName) {
this.fieldShowName = fieldShowName;
}

public String getFieldModel() {
return fieldModel;
}

public void setFieldModel(String fieldModel) {
this.fieldModel = fieldModel;
}

public String getNexFieldRelation() {
return nexFieldRelation;
}

public void setNexFieldRelation(String nexFieldRelation) {
this.nexFieldRelation = nexFieldRelation;
}
}

package com.chinaredstar.marketing.entity;

import com.chinaredstar.marketing.dto.tagJson.FieldJson;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

/**
* 公司
*/
@Entity
@Table(name = "mar_company")
public class CompanyEntity implements Serializable {

@Id
@GeneratedValue
private Long id;

@Column(name = "create_time")
private Timestamp createTime;

@Column(name = "update_time")
private Timestamp updateTime;

@Column(columnDefinition = "int default 0")
private int version;

/**
* 公司名称
*/
@Column(name = "company_name", columnDefinition = "VARCHAR(255) COMMENT '公司名称'")
private String companyName;

/**
* 公司唯一key
*/
@Column(name = "company_key", columnDefinition = "VARCHAR(255) COMMENT '公司唯一key'")
private String companyKey;

/**
* 相关业务联系人
*/
@Column(name = "conn_name", columnDefinition = "VARCHAR(255) COMMENT '相关业务联系人'")
private String connName;

/**
* 相关业务人联系电话
*/
@Column(name = "conn_phone", columnDefinition = "VARCHAR(255) COMMENT '相关业务人联系电话'")
private String connPhone;

/**
* 测试json存储
* 具体数据结构参考FieldJsonListDto
*/
@Column(name = "test_json", columnDefinition = "json COMMENT '测试json存储'")
private String testJson;

@Transient
private List<FieldJson> fieldJsonList;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Timestamp getCreateTime() {
return createTime;
}

public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}

public Timestamp getUpdateTime() {
return updateTime;
}

public void setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
}

public int getVersion() {
return version;
}

public void setVersion(int version) {
this.version = version;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public String getCompanyKey() {
return companyKey;
}

public void setCompanyKey(String companyKey) {
this.companyKey = companyKey;
}

public String getConnName() {
return connName;
}

public void setConnName(String connName) {
this.connName = connName;
}

public String getConnPhone() {
return connPhone;
}

public void setConnPhone(String connPhone) {
this.connPhone = connPhone;
}

public String getTestJson() {
return testJson;
}

public void setTestJson(String testJson) {
this.testJson = testJson;
}

public List<FieldJson> getFieldJsonList() {
return fieldJsonList;
}

public void setFieldJsonList(List<FieldJson> fieldJsonList) {
this.fieldJsonList = fieldJsonList;
}
}
常规类型bean-CompanyEntity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.chinaredstar.marketing.entity;

import com.chinaredstar.marketing.dto.tagJson.FieldJson;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

/**
* 公司
*/
@Entity
@Table(name = "mar_company")
public class CompanyEntity implements Serializable {

@Id
@GeneratedValue
private Long id;

@Column(name = "create_time")
private Timestamp createTime;

@Column(name = "update_time")
private Timestamp updateTime;

@Column(columnDefinition = "int default 0")
private int version;

/**
* 公司名称
*/
@Column(name = "company_name", columnDefinition = "VARCHAR(255) COMMENT '公司名称'")
private String companyName;

/**
* 公司唯一key
*/
@Column(name = "company_key", columnDefinition = "VARCHAR(255) COMMENT '公司唯一key'")
private String companyKey;

/**
* 相关业务联系人
*/
@Column(name = "conn_name", columnDefinition = "VARCHAR(255) COMMENT '相关业务联系人'")
private String connName;

/**
* 相关业务人联系电话
*/
@Column(name = "conn_phone", columnDefinition = "VARCHAR(255) COMMENT '相关业务人联系电话'")
private String connPhone;

/**
* 测试json存储
* 具体数据结构参考FieldJsonListDto
*/
@Column(name = "test_json", columnDefinition = "json COMMENT '测试json存储'")
private String testJson;

@Transient
private List<FieldJson> fieldJsonList;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Timestamp getCreateTime() {
return createTime;
}

public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}

public Timestamp getUpdateTime() {
return updateTime;
}

public void setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
}

public int getVersion() {
return version;
}

public void setVersion(int version) {
this.version = version;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public String getCompanyKey() {
return companyKey;
}

public void setCompanyKey(String companyKey) {
this.companyKey = companyKey;
}

public String getConnName() {
return connName;
}

public void setConnName(String connName) {
this.connName = connName;
}

public String getConnPhone() {
return connPhone;
}

public void setConnPhone(String connPhone) {
this.connPhone = connPhone;
}

public String getTestJson() {
return testJson;
}

public void setTestJson(String testJson) {
this.testJson = testJson;
}

public List<FieldJson> getFieldJsonList() {
return fieldJsonList;
}

public void setFieldJsonList(List<FieldJson> fieldJsonList) {
this.fieldJsonList = fieldJsonList;
}
}
常规类型controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.chinaredstar.marketing.controller;

import com.chinaredstar.marketing.dto.CommonResult;
import com.chinaredstar.marketing.entity.CompanyEntity;
import com.chinaredstar.marketing.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mng/company")
public class CompanyController {

@Autowired
private CompanyService companyService;

@RequestMapping(value = "/save", method = RequestMethod.POST)
public CommonResult save(CommonResult commonResult
, CompanyEntity companyEntity) {
return commonResult.setData(companyService.save(companyEntity));
}

}

以上,在此记录。