首页 > 解决方案 > 尝试使用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。

标签: javafirebasespring-bootgoogle-cloud-firestorespring-restcontroller

解决方案


这个错误是不言自明的,你可以在 Stack Overflow 上找到许多其他类似的情况。例如,您可以使用将您的收藏转换为列表Collections.singletonList()


推荐阅读