我正在使用Stomp编写WebSocket客户端,当我使用Stomp客户端发送请求时,日志输出为:
15:17:44.688]-[clientInboundChannel-59]-[org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler]-{Searching methods to handle SEND /app/vehicle session=qnlerizz}
15:17:44.688]-[clientInboundChannel-59]-[org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler]-{No matching methods.}
这表明客户端没问题。并且服务器找不到匹配的URL来处理请求。但我已经有了一个句柄方法。这是代码:
@Controller
@Log4j2
public class WebSocketController {
public SimpMessagingTemplate template;
@Autowired
public WebSocketController(SimpMessagingTemplate template) {
this.template = template;
}
@MessageMapping("/vehicle")
@SendTo("/topic/location")
public void getloc() throws Exception {
try {
for (int i = 0; i < 10; i++) {
template.convertAndSend("/topic/location", "aaaaaaa");
}
} catch (Exception e) {
log.error(e);
}
}
}
这是我的网络配置:
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/vehicle").withSockJS();
}
}
这是客户端代码:
function connect() {
var socket = new SockJS('/clbs/vehicle');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/location', function (greeting) {
showGreeting(JSON.parse(greeting.body).content);
});
});
}
stompClient.send("/app/vehicle",{},JSON.stringify("bbb"));
我该怎么办?哪里可能出错?
PS:控制器WebSocketController
已经自动扫描。
JDK:1.8 Tomcat的:8.0.36 弹簧:4.2.6
我在这个问题上奋斗了2天。
答案 0 :(得分:0)
确保控制器已成功自动扫描。我因为bean未扫描而引发此问题。因此无法匹配属性方法来处理stomp客户端请求。检查自动扫描配置。查看历史日志内容,总结的细节是here。