我的问题在于杰克逊 FasterXML 库中实现的 JsonSerialization 领域。我有一系列端点,通过这些端点在后端和 MVVM 前端框架之间交换内容。这是有效的,但现在我有点卡住了,因为我要处理用户创建/注册。
这是代表我的应用程序中的组的模型(实体)(我省略了不相关的导入声明和 JPA 注释):
@JsonRootName(value="userGroup")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Groups extends MetaInfo implements Serializable {
private String groupName;
private Set<Credential> credentials = new HashSet<>();
public Groups() {
super();
}
public Groups(String groupName) {
this();
this.groupName = groupName;
}
public Groups(String createdBy, String groupName) {
this();
setCreatedBy(createdBy);
this.groupName = groupName;
}
@JsonGetter("group_Name")
// @JsonValue
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
updateModified();
}
@JsonIgnore
public Set<Credential> getCredentials() {
return credentials;
}
public void setCredentials(Set<Credential> credentials) {
this.credentials = credentials;
}
public void addCredential(Credential c) {
credentials.add(c);
if (c.getGroup() != this) {
c.setGroup(this);
}
}
@Override
public String toString() {
return "Groups{" + "groupName=" + groupName + '}';
}
}
这是端点中的方法,它检索(如果存在)并将组的序列化版本返回给 JavaScript 客户端:
@Path("/groups")
@Produces("application/json")
public class GroupsResourceService extends RimmaRestService{
@Inject
@Production
private GroupsRepository groupsRepository;
...
@GET
@Path("{group}")
public Response getGroup(@PathParam("group") String group){
if(InputValidators.stringNotNullNorEmpty.apply(group)){
//proceed with making call to the repo
Optional<Groups> optGroup = ofNullable(groupsRepository.getByGroupName(group));
if(optGroup.isPresent()){//ultimately success scenario
try {
String output = getMapper().writeValueAsString(optGroup.get());
return Response.ok(output).build();
} catch (JsonProcessingException e) {
logger.error("Serialization error: "+e.getMessage()+
"\n"+e.getClass().getCanonicalName());
throw new InternalServerErrorException("Server error "
+ " serializing the requested group \""+group+"\"");
}
} else{
throw new NotFoundException("Group " + group + " could not be found");
}
}else{//empty space after the slash
throw new BadRequestException("You haven't provided a group parameter",
Response.status(Response.Status.BAD_REQUEST).build());
}
}
}
尝试像这样测试此代码:
@Test
public void successfullResponse(){
Response success = groupsResourceService.getGroup("admin");
assertTrue(success.getStatus()==200);
}
...残酷地失败了:
<< ERROR!
javax.ws.rs.InternalServerErrorException: Server error serializing the requested group "admin"
at com.vgorcinschi.rimmanew.rest.services.GroupsResourceService.getGroup(GroupsResourceService.java:54)
at com.vgorcinschi.rimmanew.services.GroupsResourceServiceTest.successfullResponse(GroupsResourceServiceTest.java:48)
不过,在这种情况下,堆栈跟踪的帮助为 0 - 这就是我粘贴捕获底层 Json 异常的日志输出的原因:
15:05:05.857 [main] ERROR com.vgorcinschi.rimmanew.rest.services.GroupsResourceService - Serialization error: Can not write a field name, expecting a value
com.fasterxml.jackson.core.JsonGenerationException
在访问并分析了 13 个类似的投诉链接(其中 3 个来自 stackoverflow)后,我想出了一个解决方案,它更像是一种解决方法——如果你回顾一下实体,我已经评论了@JsonValue
。如果我取消注释并注释,@JsonGetter("group_Name")
则测试通过,输出如下:
{"userGroup":"admin"}
这只是一种解决方法,我决定再次寻求帮助,我希望有人能够并且足够友善地提供。