我的问题在于Jackson FasterXML Library中实现的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"}
这只是一种解决方法,我决定再次寻求帮助,我希望有人能够提供帮助。