gpt4 book ai didi

java - 显示 "null"用于位置消息和发件人

转载 作者:行者123 更新时间:2023-12-04 10:40:43 25 4
gpt4 key购买 nike

我在 vaadin 14 和 spring boot 上创建了一个聊天。

当我开始聊天时,我必须登录并按名称登录。进入聊天室的每个人都可以在授权后写消息。

我有一个问题,当我发送消息时,聊天显示(null:null)这是错误的,我需要显示发件人的姓名和他的消息

我通过调试器,在 json 中回答一切正常输出
enter image description here

但是当我在那里打开聊天窗口时,而不是消息和发件人向我显示“null:null”)

enter image description here

主 View 类

public class MainView extends VerticalLayout {
private final MessagesInfoManager messagesInfoManager;
private final RestService restService;
private String username;
private TextField textField;
private Message messageLast;


@Autowired
public MainView(RestService restService) {
this.messagesInfoManager = MessageConfigurator.getInstance().getChatMessagesInfoManager();
addClassName("main-view");
setSizeFull();
setDefaultHorizontalComponentAlignment(Alignment.CENTER);

H1 header = new H1("Vaadin Chat");
header.getElement().getThemeList().add("dark");

add(header);

askUsername();
this.restService = restService;
}

private void askUsername() {
HorizontalLayout layout = new HorizontalLayout();
TextField usernameField = new TextField();
Button startButton = new Button("Start chat");

layout.add(usernameField, startButton);

startButton.addClickListener(click -> {
username = usernameField.getValue();
remove(layout);
showChat(username);
});

add(layout);
}

private void showChat(String username) {
MessageList messageList = new MessageList();

List<Message> lasts = restService.getLast();
for (Message message : lasts) {
messageList.add(new Paragraph(message.getFromV() + ": " + message.getMessageV()));
}

messageLast = lasts.get(lasts.size() - 1);

add(messageList, createInputLayout(username, messageList));
expand(messageList);
}

private Component createInputLayout(String username, MessageList messageList) {
HorizontalLayout layout = new HorizontalLayout();
layout.setWidth("100%");

TextField messageField = new TextField();
messageField.addKeyDownListener(Key.ENTER, keyDownEvent -> sender(messageField, messageList));
Button sendButton = new Button("Send");
sendButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

layout.add(messageField, sendButton);
layout.expand(messageField);

messageField.addFocusListener(event -> {
for (Message message : messagesInfoManager.getMessagesByUI(getUI())) {
if (!message.getFromV().equals(username)) {
message.setUnread(false);
this.restService.updateMessage(message.getId(), message);
}
}
});

sendButton.addClickListener(click -> sender(messageField, messageList));
messageField.focus();

return layout;
}

private void sender(TextField textField, MessageList messageList) {
Message message = new Message(username, textField.getValue());
message = restService.saveMessage(message);
messageLast = message;
this.textField = textField;
// messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, message, this));
textField.clear();
textField.focus();

}

@Scheduled(fixedDelay = 1000)
public void scheduleFixedDelayTask() {

MessageList messageList = new MessageList();

List<LinkedHashMap> lasts = restService.getUnreadMessages(messageLast);

for (LinkedHashMap message : lasts) {
messageList.add(new Paragraph(message.get("fromV") + ": " + message.get("messageV")));
}

messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, messageLast, this));

}

}

类(class)留言
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private Timestamp time;

@Column(name = "fromV")
private String fromv;
@Column(name = "messageV")
private String messagev;
private boolean unread;

public Timestamp getTime() {
return time;
}

public void setTime(Timestamp time) {
this.time = time;
}

public Message() {
this(null, null);
}

public Message(String from, String message) {
this.fromv = from;
this.messagev = message;
unread = true;
time = new Timestamp(System.currentTimeMillis());
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getFromV() {
return fromv;
}

public void setFromV(String fromV) {
this.fromv = fromV;
}

public String getMessageV() {
return messagev;
}

public void setMessageV(String messageV) {
this.messagev = messageV;
}

public boolean isUnread() {
return unread;
}

public void setUnread(boolean unread) {
this.unread = unread;
}

@Override
public String toString() {
return "Message{" +
"id=" + id +
", time=" + time +
", fromv='" + fromv + '\'' +
", messagev='" + messagev + '\'' +
", unread=" + unread +
'}';
}
}

我的github

https://github.com/adk96/vaadin-chat

最佳答案

json 属性名称与 Message 的字段名称不匹配类(class)。方法Gson.fromJson(...)在 json 中看到字段 fromV, messageV ,但没有名为 fromV 的属性和 messageV (因为它们被称为 fromvmessagev )。

您有几种方法可以解决这个问题:

1) 将您的字段重命名为正确的驼峰命名,以及它们的 getter/setter。

private String messageV;

public String getMessageV(){...}
publis void setMessageV(String messageV){...}

或者

2)更改创建的json,使其属性名称中没有camelCase。通过更改 Message:: toString 的输出或通过使用正确/自动序列化(例如 Gson.toJson )。

或者

3) 用 @JsonProperty 注释您的属性用于告诉 gson 该属性在 json 中具有不同名称的注释。但是,getter/setter 必须与实际的属性名称匹配。
@JsonProperty("messageV")
private String messagev;

public String getMessagev(...)
public void setMessagev(String messagev){...}

关于java - 显示 "null"用于位置消息和发件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59942149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com