Java 将表单数据绑定为JSON字符串/字符串数组以请求字符串中的参数
我有一个表单,我想通过ajax发送给我的控制器:Java 将表单数据绑定为JSON字符串/字符串数组以请求字符串中的参数,java,json,ajax,spring,spring-boot,Java,Json,Ajax,Spring,Spring Boot,我有一个表单,我想通过ajax发送给我的控制器: <div class="row"> <label for="username" class="col-sm-2 col-form-label my-auto">Username:</label> <div class="col-sm-10 my-auto">
<div class="row">
<label for="username" class="col-sm-2 col-form-label my-auto">Username:</label>
<div class="col-sm-10 my-auto">
<input type="text" class="form-control" id="username" name="username">
</div>
</div>
<div class="row">
<label for="password" class="col-sm-2 col-form-label my-auto">Password:</label>
<div class="col-sm-10 my-auto">
<input type="text" class="form-control" id="password" name="password">
</div>
</div>
<div class="row" id="ratingRow">
<label for="adminFoci" class="col-sm-2 col-form-label my-auto">User Rating:</label>
<div class="col-sm-auto my-auto">
<select class="selectpicker" multiple data-live-search="true" name="adminFoci" id="adminFoci" title="Choose Foci">
<c:forEach items="${foci}" var="focus">
<option name="${focus.focusName}" value="">${focus.focusName}</option>
</c:forEach>
</select>
</div>
<div class="col-sm my-auto">
<input type="text" class="form-control" id="rating" name="rating">
</div>
<div class="col-sm-auto my-auto" id="ratingButtonRow">
<div class="btn btn-info btn-sm" id="adminAddGroup">Add Rating</div>
</div>
</div>
<div class="row">
<label for="adminUserGroups" class="col-sm-2 col-form-label my-auto">User's Groups:</label>
<div class="col-sm-auto my-auto">
<select class="selectpicker" multiple data-live-search="true" id="adminUserGroups" name="groups" title="Choose Groups">
<c:forEach items="${groups}" var="group">
<option name="${group.groupName}" value="${group.groupName}">${group.groupName}</option>
</c:forEach>
</select>
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label my-auto">User's Roles:</label>
<div class="col-sm-10 my-auto">
<select class="selectpicker" multiple data-live-search="true" id="adminUserRoles" name="roles" title="Choose Roles">
<c:forEach items="${user.roles}" var="role">
<option name="${role.name}" value="${role.name}">${role.name}</option>
</c:forEach>
</select>
</div>
</div>
然后,我尝试将json字符串数组的数据绑定到控制器中的请求参数:
@PostMapping(value="/AdminCreateUser", consumes = MediaType.APPLICATION_JSON_VALUE)
public void createUser(@RequestParam String username, @RequestParam String password, @RequestParam (required = false) String[] roles,
@RequestParam (required = false) String[] groups){
User user = new User(username, password);
if(roles != null){
Set<Role> roleList = roleService.findRoles(roles);
if(roleList.size() > 0){
user.setRoles(roleList);
}
}
if(groups != null){
Set<Group> groupList = groupService.findGroups(groups);
for(Group group: groupList){
group.addMember(user);
groupRepository.save(group);
}
}
userRepository.save(user);
}
@PostMapping(value=“/AdminCreateUser”,consumes=MediaType.APPLICATION\u JSON\u value)
public void createUser(@RequestParam String username、@RequestParam String password、@RequestParam(required=false)String[]角色,
@RequestParam(必需=false)字符串[]组){
用户=新用户(用户名、密码);
if(角色!=null){
设置roleList=roleService.findRoles(角色);
如果(roleList.size()>0){
user.setRoles(角色列表);
}
}
如果(组!=null){
设置groupList=groupService.findGroups(组);
对于(组:组列表){
group.addMember(用户);
groupRepository.save(组);
}
}
userRepository.save(用户);
}
但是我得到了
解析[org.springframework.web.bind.MissingServletRequestParameterException:所需字符串参数“username”不存在]
。Jackson没有将json字符串数组的匹配元素绑定到请求参数吗?如果没有,我该怎么做?我想省去将每个表单字段作为单独数据发送的工作序列化数组的输出将是
[
{
"name" : "username",
"value" : "test"
},...
]
然后将java代码更改为
public void createUser(@RequestBody String body){
将字符串转换为JSONObject并将其作为JSON处理。这样就行了。
如果要处理请求参数。
将内容类型更改为多部分/表单数据,并将其直接发布到API。有两种方法
$("#modalForm").submit(function(e){
e.preventDefault();
var form = $(this);
//convert form data to json array of Object(name value pair)
data = []
var x = $(this).serializeArray();
$.each(x, function(i, field){
var name = field.name; var value = field.value;
data[i] = {name : value};
});
$.ajax({
type: "POST",
url: form.attr('action'),
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
success: function(data)
{
table.row('.selected').ajax.reload();
}
});
});
修改控制器
public void createUser(@RequestBody CreateUserDto createUserRequest){
我认为使用DTO对我不起作用。表单包含的值与用户实体没有直接关系,因此无法将请求主体映射为将DTO映射到实体,即使它具有不同的字段。您可以使用MapStruct,也可以根据需要使用getter和process从DTO获取值。如果我将其转换为字符串,我无法拆分每个字段值,因为某些字段可能存在,也可能不存在(required=false)。您可以使用name字段在服务器端验证该值?
public void createUser(@RequestBody CreateUserDto createUserRequest){