我正在使用Spring Boot,Websockets开发一个聊天框应用程序,并尝试保存在mongoDB数据库中。应用程序运行成功!但是通过将错误作为主键异常抛出,聊天记录未存储在数据库中。我不知道为什么会这样。
这部分属于配置类,其中该类的配置为dong:
@Configuration
@EnableWebSocketMessageBroker
public class WsConfig implements WebSocketMessageBrokerConfigurer{
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/autoNext").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/app");
}
}
This is controller class for the messenger application
@Controller
public class ChatController {
@Autowired
private ChatService chatService;
@MessageMapping("/chat.register")
@SendTo("/topic/public")
public ChatMessage register(@Payload ChatMessage chatMessage, SimpMessageHeaderAccessor headerAccessor) {
headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());
System.out.println(chatMessage);
chatService.saveChat(chatMessage);
return chatMessage;
}
@MessageMapping("/chat.send")
@SendTo("/topic/public")
public ChatMessage sendMessage(@Payload ChatMessage chatMessage) {
System.out.println(chatMessage);
chatService.saveChat(chatMessage);
return chatMessage;
}
}
This is chat entity where in MYSQL entity class:
@Document(collection="chat_collection")
public class ChatEntity {
@Id
private int id;
@Field("content")
private String content;
@Field("sender")
private String sender;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
This is Service class
@Service
public class ChatService {
@Autowired
private ChatRepository chatRepository;
public void saveChat(ChatMessage chatMessage) {
ChatEntity chatEntity=new ChatEntity();
BeanUtils.copyProperties(chatMessage, chatEntity);
chatRepository.insert(chatEntity);
}
}
This is Repository
@Repository
public interface ChatRepository extends MongoRepository <ChatEntity, Integer> {
}
error log:
Caused by: com.mongodb.MongoWriteException: E11000 duplicate key error collection: ChatBox.chat_collection index: _id_ dup key: { _id: 0 }
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:967) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:494) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:478) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:472) ~[mongodb-driver-3.8.2.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate$6.doInCollection(MongoTemplate.java:1436) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:538) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
... 52 common frames omitted
2020-02-19 16:36:36.030 INFO 21228 --- [MessageBroker-2] o.s.w.s.c.WebSocketMessageBrokerStats : WebSocketSession[1 current WS(1)-HttpStream(0)-HttpPoll(0), 1 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(1)-CONNECTED(1)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 8, active threads = 0, queued tasks = 0, completed tasks = 18], outboundChannel[pool size = 2, active threads = 0, queued tasks = 0, completed tasks = 2], sockJsScheduler[pool size = 4, active threads = 1, queued tasks = 2, completed tasks = 10]