在我的应用程序中,我使用webSocket,我需要从客户端传输对象到服务器。服务器端方法如下所示:
@MessageMapping("/chat/{usersSelected}")
public void disperseMessage(@DestinationVariable("usersSelected") List<UserSelected> usersSelected) {...
客户端对象是这样的:
vm.usersSelected = [];
var userSelected = {};
userSelected.userId = userId;
userSelected.institutionId = institutionId;
vm.usersSelected.push(userSelected);
我得到以下异常:
org.springframework.core.convert。ConverterNotFoundException:没有找到能够从java.lang.String类型转换为@org.springframework.messaging.handler.annotation类型的转换器。DestinationVariable并不知道
有没有人知道我如何解决这个问题(我使用Spring Boot) -有一个注释来防止异常?非常感谢!
[编辑]
服务器方法vm.stompClient.send("/app/chat/" + vm.institutionIdOfSender + "/" + vm.userIdOfSender + "/" + vm.usersSelected + "/" + confirmation, {}, JSON.stringify({ 'name': vm.name, 'id': vm.userIdOfSender, 'message': message }));
Client -> Server方法调用:
@MessageMapping("/chat/{institutionIdOfSender}/{userIdOfSender}/{usersSelected}/{confirmation}")
public void disperseMessage(@DestinationVariable("institutionIdOfSender") String institutionIdOfSender,
@DestinationVariable("userIdOfSender") String userIdOfSender, @DestinationVariable("usersSelected") String usersSelected,
@DestinationVariable("confirmation") Boolean confirmation, final ChatMessage message) throws Exception {
[EDIT2] 我已经用这种方法解决了它,但它不是一个很好的解决方案:
服务器 @MessageMapping("/chat/{institutionIdOfSender}/{userIdOfSender}/{usersSelected}/{confirmation}/{chatMessageFilter}")
public void disperseMessage(@DestinationVariable("institutionIdOfSender") String institutionIdOfSender,
@DestinationVariable("userIdOfSender") String userIdOfSender, @DestinationVariable("usersSelected") String usersSelectedString,
@DestinationVariable("confirmation") Boolean confirmation,
@DestinationVariable("chatMessageFilter") ChatMessageFilterEnum chatMessageFilter, final ChatMessage message) throws Exception {
// transfer JSON- string into List- Objects:
final List<UserSelected> usersSelected = new ObjectMapper().readValue(usersSelectedString,
new TypeReference<List<UserSelected>>() {});
在客户:vm.stompClient.send("/app/chat/" + vm.institutionIdOfSender + "/" + vm.userIdOfSender + "/" + JSON.stringify(vm.usersSelected) + "/" + confirmation + "/SELECTED_USERS", {}, JSON.stringify({ 'name': vm.name, 'id': vm.userIdOfSender, 'message': message }));
我有机会不转换JSON字符串在服务器上由我自己吗?(这应该在幕后进行)谢谢你的帮助!
@DestinationVariable不指向消息主体,而是路径中String的占位符。
。(使用与代码相同的模式)
@MessageMapping("/chat/{chatChannel}")
public void disperseMessage(@DestinationVariable("chatChannel") String chatChannel) {...
如果我要发送消息到/chat/allChat, chatChannel的值将是字符串"allChat"
所以错误的意思是它不能将字母"allChat"转换为列表,因为"allChat"不会是足够的信息(除非你创建了一个转换器,从一个频道抓取用户列表,这可能不是你想要的,可能更有意义作为一个服务)
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
春天。io有一个关于WebSockets的很棒的Spring Boot教程,其中展示了一个示例服务,该服务使用可用的json解析器获取消息体。所以我只是调整了教程中的代码来适合你。
Javascriptvar message = {
'chatMessage': { 'name': vm.name, 'id': vm.userIdOfSender, 'message': message },
'usersSelected': vm.usersSelected
}
vm.stompClient.send("/app/chat/" + vm.institutionIdOfSender + "/" + vm.userIdOfSender + "/" + confirmation + "/SELECTED_USERS", {}, JSON.stringify(message));
Java -随意命名类ChatMessageAndUsersSelected更好
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
@Controller
public class GreetingController {
@MessageMapping("/chat/{institutionIdOfSender}/{userIdOfSender}/{usersSelected}/{confirmation}/{chatMessageFilter}")
public void disperseMessage(@DestinationVariable("institutionIdOfSender") String institutionIdOfSender,
@DestinationVariable("userIdOfSender") String userIdOfSender,
@DestinationVariable("confirmation") Boolean confirmation, final ChatMessageAndUsersSelected message) throws Exception {
//Do stuff here.
}
}
https://spring.io/guides/gs/messaging-stomp-websocket/