一、问题重现:
原代码:用get方式传递@ResponseBody
@RequestMapping(value = {"/findGroupByGroupName/{batchNo}/{groupSex}/{groupName}"}, method = RequestMethod.GET)
@ApiOperation(value = "模糊搜索查询分组情况是否成功", notes = "输入分组的批次,分组的性别、分组的部分名称", tags = {"查询"})
public ItooResult findGroupByGroupName(@ApiParam(name = "batchNo", value = "分组的批次", required = true) @PathVariable String batchNo, @ApiParam(name = "groupSex", value = "分组的性别", required = true) @PathVariable String groupSex, @ApiParam(name = "groupName", value = "分组的部分名称", required = true) @PathVariable String groupName) {
.....
}
url传递“groupName”为中文时,会显示乱码:
二、解决办法:
url传参去掉“groupName”参数,改为POST方式传参,为“groupName”加上@RequestBody 注解。
修改后代码:@ResponseBody
@RequestMapping(value = {"/findGroupByGroupName/{batchNo}/{groupSex}"}, method = RequestMethod.POST)
@ApiOperation(value = "模糊搜索查询分组情况是否成功", notes = "输入分组的批次,分组的性别、分组的部分名称", tags = {"查询"})
public ItooResult findGroupByGroupName(@ApiParam(name = "batchNo", value = "分组的批次", required = true) @PathVariable String batchNo, @ApiParam(name = "groupSex", value = "分组的性别", required = true) @PathVariable String groupSex, @ApiParam(name = "groupName", value = "分组的部分名称", required = false) @RequestBody String groupName) {
......
1}
前端代码:用post传递body形式selectGroup() {
searchGroup: string;
selectGroupUrl = 'physical-web/group/findGroupByGroupName/' + batchNo + '/' + this.studentSex;
let body = searchGroup;
this.http.post(selectGroupUrl, body).subscribe(
res => {
if (res.json().code == '0000' && res.json().data.length > 0) {
this.searchGroup = res.json().data;
for (let i = 0; i < this.searchGroup.length; i++) {
this.group.id = this.searchGroup[i].id;
this.group.groupName = this.searchGroup[i].groupName;
}
}
else {
mui.alert("查询结果为空,请重新输入查询条件");
}
}
)
}
url 传递的值:
问题解决!
本文讲述了如何在API接口中,当URL参数为中文时遇到乱码问题,通过将GET请求改为POST并使用@RequestBody处理,成功解决了这个问题。作者提供了前后代码对比和前端调用示例,适合前端开发者和API设计者参考。
4353

被折叠的 条评论
为什么被折叠?



