【发布时间】:2021-12-22 00:53:41
【问题描述】:
一个 api 返回 {"authorities": [{"authority": "Employee"}]}
我想删除 [ ]
@RequestMapping(value=/"autho", method="GET", produces=APPLICATION_JSON_VALUE)
public ResponseEntity getAuthorities(final User usr){
return ResponseEntity.ok(new GetAuthorityResponse(usr.getAuthorities()));
}
这里的 getAuthorities() 来自 Collection<GrantedAuthority> 类型的 org.springframework.security。
GetAuthorityResponse 从Collection<GrantedAuthority> 转换为Collection<CustomAuthority>。
CustomAuthority 是一个枚举。
@JsonFormat(shape=JsonFormat.Shape.OBJECT)
public enum CustomAuthority implements GrantedAuthority{
EMPLOYEE("EMPLOYEE"), EMPLOYEE1("EMPLOYEE1");
private final string authority;
// Constructor and getter
}
更新:此错误已在之前的 sprint 中解决。它被错误地重新分配。
【问题讨论】:
-
如果响应可能返回多个权限,为什么要删除集合层?
-
名称
getAuthorities表示会返回多个值,所以需要一个集合。如果您只想返回一个授权,请更改数据模型。 -
@Thomas 删除收集层是指括号吗?如果是,那么只是一个来自积压的错误修复,其中 json 响应不应该是一个数组。
-
@f1sh 感谢您的评论。但它可以返回多个值。
-
那么您不能简单地删除
[ ],因为它们代表 JSON 中的列表。
标签: java json spring spring-security