java - 尝试使用spring boot将相同的数据保存到firebase
问题描述
我收到此错误:
java.lang.IllegalArgumentException: Could not serialize object. Serializing Collections is not supported, please use Lists instead (found in field 'user.subsMsg')
at com.google.cloud.firestore.CustomClassMapper.serializeError(CustomClassMapper.java:555) ~[google-cloud-firestore-1.21.0.jar:1.21.0]
尝试将数据添加到 Firebase 时。
这是后请求:
@PostMapping("/pollAdd")
public String pollAdd(@RequestParam String name, @RequestParam String description, @RequestParam String isPublic,
@RequestParam String status, Model model, OAuth2AuthenticationToken authentication) {
Optional<User> userOpt = userRepository.findById(main.getUser(client));
if (!userOpt.isPresent()) {
return "index";
}
User user = userOpt.get();
model.addAttribute("name", getUserName(authentication));
Poll newPoll = new Poll();
if(description.isBlank() && name.isBlank()) {
model.addAttribute("message", "Fill all attributes and try again!");
return "pollCreate";
}
newPoll.setDescription(description);
newPoll.setName(name);
newPoll.setPublic(Boolean.parseBoolean(isPublic));
newPoll.setStatus(status);
newPoll.setUser(user);
pollRepository.save(newPoll);
CollectionReference pollCR = fb.getFirebase().collection("Polls");
pollCR.document(String.valueOf(newPoll.getPollID())).set(newPoll); #This is where the problem occur.
model.addAttribute("polls", newPoll);
model.addAttribute("name", getUserName(authentication));
return "index";
}
这是民意调查类:
@Data
@Entity
public class Poll {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Setter(AccessLevel.PROTECTED)
private int pollID;
private String name;
private String description;
private boolean isPublic;
private int voteGreen;
private int voteRed;
private String status;
private int timeLimit;
@ManyToOne
private User user;
@ManyToMany(cascade = CascadeType.PERSIST)
private List<User> usersVoted = new ArrayList<>();
public Poll() {
}
public Poll(String name, String description, boolean isPublic, int voteGreen, int voteRed, String status,
int timeLimit, User user) {
this.name = name;
this.description = description;
this.isPublic = isPublic;
this.voteGreen = voteGreen;
this.voteRed = voteRed;
this.status = status;
this.timeLimit = timeLimit;
this.user = user;
}
public void setUsersVoted(User userVoted) {
this.usersVoted.add(userVoted);
}
public void setUser(User user) {
this.user = user;
if (user != null)
user.setPolls(this);
}
public void setVoteRed(int red) {
this.voteRed += red;
}
public void setVoteGreen(int green) {
this.voteGreen += green;
}
public void setPublic(boolean isPublic) {
this.isPublic = isPublic;
}
}
我正在尝试将添加到 H2 数据库的相同数据同时添加到 firebase 数据库中。我该怎么做呢?这是错的吗?我尝试将@IgnoreExtraProperties 添加到我的班级以及@Exclude。
解决方案
这个错误是不言自明的,你可以在 Stack Overflow 上找到许多其他类似的情况。例如,您可以使用将您的收藏转换为列表Collections.singletonList()
推荐阅读
- flutter - 如何在 listView.builder 中找到可以跳转到的最后一个值?
- c++ - 刽子手游戏 - 如何在 C++ 中做“错误猜测”?
- azul-zulu - Azul Zulu JRE 中 crs-agent.jar 的用途是什么?
- r - 如何使 R 内核可用于 jupyter?
- windows - 如何将 Oracle sqlplus spool 查询结果保存到文件中?
- c - 如何使用 AT 命令在 u-blox sara r410 上“清除非易失性内存”
- python - 需要帮助将 numpy 数组图像转换为全彩色图像
- javascript - 如何在文件之外读取 mocha 测试套件/案例名称?
- r - R 表示包版本与 conda 不同
- c# - 使用 linq 在 c# 中将 GeoData 转换为 GeoJson